php数组分组问题
发布于: 2015-08-24 09:17:23 )
<?php
$order_item = Array (
=> Array ( 'order_id' => 1111 ,'item_id' => 1, 'pro_id' => 1, 'pro_name' => 1111 ,'buy_num' => 5),
=> Array ( 'order_id' => 1111 ,'item_id' => 2 ,'pro_id' => 2 ,'pro_name' => 2222, 'buy_num' => 5 ),
=> Array ( 'order_id' => 2222 ,'item_id' => 3 ,'pro_id' => 1 ,'pro_name' => 2222, 'buy_num' => 1 ),
=> 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);