标签:
<?php $arr = array( array(‘id‘ => 1, ‘name‘ => ‘seu‘,‘brand‘=>‘临‘), array(‘id‘ => 1, ‘name‘ => ‘seu‘,‘brand‘=>‘临‘), array(‘id‘ => 3, ‘name‘ => ‘njupt‘,‘brand‘=>‘兵‘), array(‘id‘ => 3, ‘name‘ => ‘njupt‘,‘brand‘=>‘斗‘), array(‘id‘ => 1, ‘name‘ => ‘seu‘,‘brand‘=>‘者‘), array(‘id‘ => 1, ‘name‘ => ‘seu‘,‘brand‘=>‘皆‘), array(‘id‘ => 1, ‘name‘ => ‘seu‘,‘brand‘=>‘皆‘), array(‘id‘ => 2, ‘name‘ => ‘peking‘,‘brand‘=>‘阵‘), array(‘id‘ => 2, ‘name‘ => ‘peking‘,‘brand‘=>‘阵‘), array(‘id‘ => 3, ‘name‘ => ‘njupt‘,‘brand‘=>‘兵‘), );
现有如上的二维数组
要求1:按照id分组
实现方法:
<?php foreach($arr as $v){ $newArr[$v[‘id‘]][] = $v; } return array_values($newArr);
要求2:按照id和brand分组
实现方法:
foreach($arr as $v){ $newArr[$v[‘id‘].$v[‘brand‘]][] = $v; } return array_values($newArr);
【2016.4.27】二维数组分组:按照一个键值,按照多个键值
标签:
原文地址:http://www.cnblogs.com/junxu/p/5440438.html