php in array收集相同的数组

时间:2017-11-16 09:26:47

标签: php arrays collect

我在PHP中有这个数组:

$a = [
['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
['name'=>'corab' ,'type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
['name'=>'corab' ,'type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
['name'=>'corab' ,'type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
['name'=>'yag' ,'type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
];

我想在一个数组中收集相同的数组。有人可以帮忙吗? 我需要一些像这样的结果

 [
  [
   ['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
   ['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
   ['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
   ['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
   ['name'=>'salvar','type'=>'Qt','count'=>'1','price'=>'10','total'=>'7']
  ],
  [
   ['name'=>'corab' ,'type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
   ['name'=>'corab' ,'type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
   ['name'=>'corab' ,'type'=>'Qt','count'=>'1','price'=>'10','total'=>'7'],
  ],
  [
   ['name'=>'yag' ,'type'=>'Qt','count'=>'1','price'=>'10','total'=>'7']
  ]
 ]

1 个答案:

答案 0 :(得分:3)

可以通过标准功能

完成
$res = [];
foreach(array_count_values(array_map('json_encode', $a)) as $k=>$v) {
    $res[] = array_fill(0, $v, json_decode($k, true));
}

<强> demo on eval.in