Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP对数组进行分组_Php_Arrays_Multidimensional Array - Fatal编程技术网

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']); }
相关的(不是的副本)相关的(不是的副本)启用严格报告时是否会生成警告?启用严格报告时是否会生成警告?