代码示例:
foreach($sourceArray as $k=>$v) { $result[$v["sendto"]][] = $v; // sendto 根据你想要的 }
上面的只是适用于简单分组,如果遇到 统一数组中有重复分组的数据,就会遇到分组重复的问题,那么就需要使用下面的示例了。
代码示例:
$content = json_decode($cart->content, true); $result = []; foreach ($content as $key => $val) { $list = [ 'goods_id' => $val['goods_id'], 'goods_title' => $val['goods_title'], 'goods_num' => $val['goods_num'], 'thumb' => $val['thumb'], 'market_price' => $val['market_price'], 'single_price' => $val['single_price'], 'updated_at' => isset($val['updated_at']) ? $val['updated_at'] : Carbon::now()->toDateTimeString(), ]; if(array_key_exists($val['company_id'],$result)){ array_push($result[$val['company_id']]['list'],$list); } else{ $result[$val['company_id']]['company_id'] = $val['company_id']; $result[$val['company_id']]['company_name'] = $val['company_name']; $result[$val['company_id']]['list'] = []; array_push($result[$val['company_id']]['list'],$list); } }
返回值示例:
"code": "1010000", "message": "获取购物车列表成功", "data": [ { "company_id": "297186", "company_name": "jinantuyi", "list": [ { "goods_id": "1", "goods_title": "测试产品", "goods_num": "1", "thumb": "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1459834649&di=43457f9142b66d631b54cc778402853d&src=http://img63.afzhan.com/2/20141209/635537374782978835445.jpg", "market_price": "100.00", "single_price": "80.00", "updated_at": "2016-04-06 11:15:42" }, { "goods_id": "3", "goods_title": "测试产品3", "goods_num": "2", "thumb": "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1459834649&di=43457f9142b66d631b54cc778402853d&src=http://img63.afzhan.com/2/20141209/635537374782978835445.jpg", "market_price": "200.00", "single_price": "130.00", "updated_at": "2016-04-06 11:05:23" } ] }, { "company_id": "284030", "company_name": "qiye002", "list": [ { "goods_id": "2", "goods_title": "测试产品2", "goods_num": "1", "thumb": "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1459834649&di=43457f9142b66d631b54cc778402853d&src=http://img63.afzhan.com/2/20141209/635537374782978835445.jpg", "market_price": "120.00", "single_price": "100.00", "updated_at": "2016-04-06 10:56:12" } ] } ] }