$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 )
);
$ar = array();
$result = array();
foreach($order_item as $k => $v) {
$ar[$v['order_id']]['items'][] = array_slice($v, 1);
}
foreach($ar as $k => $v) $result[] = array('order_id' => $k, 'items' => $v['items']);
echo '<pre style="font:10px Tahoma;">';
print_r($result);
/*
Array
(
[0] => Array
(
[order_id] => 1111
[items] => Array
(
[0] => Array
(
[item_id] => 1
[pro_id] => 1
[pro_name] => 1111
[buy_num] => 5
)
[1] => Array
(
[item_id] => 2
[pro_id] => 2
[pro_name] => 2222
[buy_num] => 5
)
)
)
[1] => Array
(
[order_id] => 2222
[items] => Array
(
[0] => Array
(
[item_id] => 3
[pro_id] => 1
[pro_name] => 2222
[buy_num] => 1
)
[1] => Array
(
[item_id] => 4
[pro_id] => 2
[pro_name] => 2222
[buy_num] => 1
)
)
)
)
*/
<?php
$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 )
);
$items = array();
foreach($order_item as $item) {
$order_id = $item['order_id'];
unset($item['order_id']);
if(!isset($items[$order_id])) {
$items[$order_id] = array('order_id'=>$order_id, 'items'=>array());
}
$items[$order_id]['items'][] = $item;
}
// 下面这一步可不要
sort($items);
print_r($items);
两种方式大同小异