一段數組為例:
$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
]
]
]
]
]
]