<?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);