我有一个数组:
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
)
)
我该如何做到这一点?
答案 0 :(得分:3)
如果我们将调用第一个数组$ start和最后一个$ finish,那么:
$finish = array(); foreach ($start as $v){ $finish[$v['customer']][] = array('itemno'=>$v['itemno'], 'price'=>$v['price']); }
答案 1 :(得分:0)
$newArray =array();
foreach($originalArray as $item){
if(!array_key_exists($item->customer, $newArray)){
$newArray[$item->customer]= array();
}
$newArray[$item->customer][] = $item;
}
//最终结果将是$ newArray = array('customer1'=> array(customers ...),'customer2'=> array(customers ...));