一段数组为例:
$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 二维数组根据某个键值 给二位数组分类】
推荐阅读
- PHP-JS-AES对称加密解密代码实现
- 循环、双向、链式、数组 一文搞懂队列
- Excel一维表转二维,没有比PQ更合适
- PHP+NGINX服务器性能安全优化
- PHP时间函数总结
- PHP中操作任意精度大小的GMP扩展学习
- 如何把微信和支付宝的收款二维码合成一个?
- JavaScript 数组操作必须熟练运用的 10 个方法
- PHP有哪些杀手级超厉害框架或库或应用?
- CentOS7下编译安装PHP8.0.7