给定一个二维数组,例如 $arr
$arr = [ 0=>['child_id'=>3,'id'=>1,'addtime'=>'32'], 1=>['child_id'=>3,'id'=>1,'addtime'=>'54'], 2=>['child_id'=>4,'id'=>1,'addtime'=>'89'], 3=>['child_id'=>1,'id'=>1,'addtime'=>'56'], 4=>['child_id'=>3,'id'=>1,'addtime'=>'34'], 5=>['child_id'=>4,'id'=>1,'addtime'=>'12'], 6=>['child_id'=>2,'id'=>1,'addtime'=>'54'] ];
$barr = [];
foreach ($arr as $key => $value) {
$barr[$value['child_id']][] = $value;
}
print_r($barr);
exit;
//下面是打印的结果
[
3 => [
0 => ['child_id' => 3,'id' => 1,'addtime' => 32],
1 => ['child_id' => 3,'id' => 2,'addtime' => 54]
],
4 => [
0 => ['child_id' => 4,'id' => 3,'addtime' => 89],
1 => ['child_id' => 4,'id' => 4,'addtime' => 12],
],
1 => [
0 => ['child_id' => 1,'id' =>5,'addtime' => 56]
],
2 => [
0 => ['child_id' => 2,'id' =>6,'addtime' => 54]
]
]
最后,希望能帮助到大家!