2])->select()->toArray(); $cityLists = []; foreach ($lists as $city){ $parent = $cityLists[$city['parent_id']] ?? []; if($parent){ $parent['sons'][] =[ 'id' => $city['city_id'], 'name' => $city['name'], ]; }else{ $parent = [ 'id' => $city['parent_id'], 'name' => $city['parent_name'], 'sons' => [ [ 'id' => $city['city_id'], 'name' => $city['name'], ], ], ]; } $cityLists[$city['parent_id']] = $parent; } return array_values($cityLists); } }