我有一个数组:
Array
(
[customer] => One
[itemno] => Yellow Ribbon
[price] => 1,2
)
Array
(
[customer] => One
[itemno] => Blue Band
[price] => 0,5
)
Array
(
[customer] => Two
[itemno] => Red Tape
[price] => 2,0
)
我想按客户进行分组,如下所示:
Array
(
[One] => Array (
[itemno] => Yellow Ribbon
[price] => 1,2
)
[itemno] => Blue Band
[price] => 0,5
)
[Two] => Array (
[itemno] => Red Tape
[price] => 2,0
)
)
我如何才能做到这一点呢?
发布于 2010-08-15 13:09:53
$newArray =array();
foreach($originalArray as $item){
if(!array_key_exists($item->customer, $newArray)){
$newArray[$item->customer]= array();
}
$newArray[$item->customer][] = $item;
}
//最终结果为$newArray =array(‘Customer1’=>数组( customers...),‘Customer2’=>数组(customers...));
https://stackoverflow.com/questions/3487468
复制