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 )
);

$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);

两种方式大同小异

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值