PHP对数组进行分组
我有一个数组:PHP对数组进行分组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个数组: Array ( [customer] => One [itemno] => Yellow Ribbon [price] => 1,2 ) Array ( [customer] => One [itemno] => Blue Band [price] => 0,5 ) Array ( [customer] => Two [itemno] => Red Tape
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
)
)
我怎样才能做到这一点
$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…)
//最终结果将是$newArray=array('customer1'=>array(customers…,'customer2'=>array(customers…) 如果调用第一个数组$start和最后一个数组$finish,那么: $finish = array(); foreach ($start as $v){ $finish[$v['customer']][] = array('itemno'=>$v['itemno'], 'price'=>$v['price']); } $finish=array(); foreach($v开始){ $finish[$v['customer']][]=array('itemno'=>$v['itemno'],'price'=>$v['price']); }
如果调用第一个数组$start和最后一个数组$finish,则: $finish = array(); foreach ($start as $v){ $finish[$v['customer']][] = array('itemno'=>$v['itemno'], 'price'=>$v['price']); } $finish=array(); foreach($v开始){ $finish[$v['customer']][]=array('itemno'=>$v['itemno'],'price'=>$v['price']); }
相关的(不是的副本)相关的(不是的副本)启用严格报告时是否会生成警告?启用严格报告时是否会生成警告?