$order_item = Array ( 0 => Array ( 'order_id' => 1111 ,'item_id' => 1, 'pro_id' => 1, 'pro_name' => 1111 ,'buy_num' => 5), 1 => Array ( 'order_id' => 1111 ,'item_id' => 2 ,'pro_id' => 2 ,'pro_name' => 2222, 'buy_num' => 5 ), 2 => Array ( 'order_id' => 2222 ,'item_id' => 3 ,'pro_id' => 1 ,'pro_name' => 2222, 'buy_num' => 1 ), 3 => Array ( 'order_id' => 2222 ,'item_id' => 4 ,'pro_id' => 2 ,'pro_name' => 2222, 'buy_num' => 1 ) );
1.一次遍历数组
$items = array(); foreach ($order_item as $value) { $order_id = $value['order_id']; unset($value['order_id']); if(!isset($items[$order_id])) { $items[$order_id] = array('order_id'=>$order_id, 'items'=>array()); } $items[$order_id]['items'][] = $value; }
2. 两次遍历数组
$items = array();$res =array();foreach ($order_item as $value) { $items[$value['order_id']]['items'][] = array_slice($value, 1);}foreach ($items as $key => $value) { $res[] = array('order_id'=>$key,'item'=>$value['items']);}unset(<span></span>items);