您的位置 首页 php

PHP 二维数组根据某个键值 给二位数组分类

一段数组为例:

 $list = array:4 [
  0 => array:7 [
    "id" => 56
    "mer_id" => 7
    "order_id" => "wx163265961408769974"
    "is_postage" => 0
    "store_name" => "奇柏丽"
    "postage" => "快递配送"
    "truePrice" => 42.0
  ]
  1 => array:7 [
    "id" => 67
    "mer_id" => 7
    "order_id" => "wx163274484223537232"
    "is_postage" => 0
    "store_name" => "奇柏丽"
    "postage" => "快递配送"
    "truePrice" => 208.0
  ]
  2 => array:7 [
    "id" => 66
    "mer_id" => 3
    "order_id" => "wx163273502881634474"
    "is_postage" => 0
    "store_name" => "美丽小铺"
    "postage" => "快递配送"
    "truePrice" => 119.0
  ]
  3 => array:7 [
    "id" => 68
    "mer_id" => 1
    "order_id" => "wx163279157705472302"
    "is_postage" => 1
    "product_attr_unique" => "d973a4f5"
    "store_name" => "七七小铺"
    "postage" => "到店自提"
    "truePrice" => 120.0
  ]
]  

在这个数组中以 到店自提(postage) 快递配送(postage) 来分类,然后在此基础上再以商家为基础分类,用到的键名(is_postage,postage,mer_id,store_name)。

php代码实现

 $new = [];
        
foreach($list as $k => $item){
  if($item['is_postage'] ==0 ){

    $new[$item['is_postage']]['postage_name'] = "快递配送";

  }else{

    $new[$item['is_postage']]['postage_name'] = "到店自提";

  }

  $new[$item['is_postage']]['postage'][$item['mer_id']]['store_name'] = $item['store_name'];
  $new[$item['is_postage']]['postage'][$item['mer_id']]['store'][$k] = $item;
}
dump($new);
order
输出结果:
array:2 [
  0 => array:2 [
    "postage_name" => "快递配送"
    "aa" => array:2 [
      7 => array:2 [
        "store_name" => "奇柏丽"
        "store" => array:2 [
          0 => array:7 [
            "id" => 56
            "mer_id" => 7
            "order_id" => "wx163265961408769974"
            "is_postage" => 0
            "store_name" => "奇柏丽"
            "postage" => "快递配送"
            "truePrice" => 42.0
          ]
          1 => array:7 [
            "id" => 67
            "mer_id" => 7
            "order_id" => "wx163274484223537232"
            "is_postage" => 0
            "store_name" => "奇柏丽"
            "postage" => "快递配送"
            "truePrice" => 208.0
          ]
        ]
      ]
      3 => array:2 [
        "store_name" => "美丽小铺"
        "store" => array:1 [
          2 => array:7 [
            "id" => 66
            "mer_id" => 3
            "order_id" => "wx163273502881634474"
            "is_postage" => 0
            "store_name" => "美丽小铺"
            "postage" => "快递配送"
            "truePrice" => 119.0
          ]
        ]
      ]
    ]
  ]
  1 => array:2 [
    "postage_name" => "到店自提"
    "aa" => array:1 [
      1 => array:2 [
        "store_name" => "七七小铺"
        "store" => array:1 [
          3 => array:7 [
            "id" => 68
            "mer_id" => 1
            "order_id" => "wx163279157705472302"
            "is_postage" => 1
            "store_name" => "七七小铺"
            "postage" => "到店自提"
            "truePrice" => 120.0
          ]
        ]
      ]
    ]
  ]
]  

文章来源:智云一二三科技

文章标题:PHP 二维数组根据某个键值 给二位数组分类

文章地址:https://www.zhihuclub.com/150904.shtml

关于作者: 智云科技

热门文章

网站地图