第一种
$result=array(); foreach($data as $k=>$v){ $name=$v['store_id']; $result[$name]['store_name']=$v['store_name']; $result[$name]['store_id']=$v['store_id']; $result[$name]['item'][]=$v; } $result = array_values($result); return [0, 200, '成功', $result];
第二种
foreach($data as $k=>$v){ $datas[$v['store_id']][] = $v; } foreach ( $datas as $k=>$v ) { $name= $v[0]['store_name']; $item = $v[0]['store_id']; // unset($v['store_name']); // unset($v['store_name']); $result[$k]['store_name']=$name; $result[$k]['store_id']=$item; $result[$k]['list']=$v; } $result = array_values($result);