php 二维数组 根据条件合并成新的二维数组
$arr = [ 0 => [ 'id' => 1, 'name' => '张三', 'phone' => '123456789' ], 1 => [ 'id' => 2, 'name' => '李四', 'phone' => '456789' ], 3 => [ 'id' => 1, 'name' => '张三1', 'phone' => '1234567890' ], 4 => [ 'id' => 1, 'name' => '张三2', 'phone' => '12345678901' ], 5 => [ 'id' => 2, 'name' => '李四1', 'phone' => '12345678666' ] ]; $arr1 = []; foreach($arr as $v){ if(!isset($arr1[$v['id']])){ $arr1[$v['id']] = [ 'id' => $v['id'], 'info' => [[ 'name' => $v['name'], 'phone' => $v['phone'], ]], ]; }else{ $arr1[$v['id']]['info'][] = [ 'name' => $v['name'], 'phone' => $v['phone'], ]; } } print_r($arr1);
function getTree($array, $pid=1){ $tree = []; foreach ($array as $key => $value) { if ($value['parent_code'] == $pid) { $value['children']= $this->getTree($array, $value['city_id']); if (empty($value['children'])) unset($value['children']); $tree[] = $value; } } return $tree; }