$data) { $refer[$data[$pk]] = &$list[$key]; } foreach ($list as $key => $data) { $parentId = $data[$pid]; if ($root == $parentId) { $tree[] = &$list[$key]; }else{ if (isset($refer[$parentId])) { $parent = &$refer[$parentId]; $parent[$child][] = &$list[$key]; } } } } return $tree; } function formatTree($list, $lv = 0, $title = 'name'){ $formatTree = array(); foreach($list as $key => $val){ $title_prefix = ''; for( $i=0;$i<$lv;$i++ ){ $title_prefix .= "|---"; } $val['lv'] = $lv; $val['namePrefix'] = $lv == 0 ? '' : $title_prefix; $val['showName'] = $lv == 0 ? $val[$title] : $title_prefix.$val[$title]; if(!array_key_exists('_child', $val)){ array_push($formatTree, $val); }else{ $child = $val['_child']; unset($val['_child']); array_push($formatTree, $val); $middle = formatTree($child, $lv+1, $title); //进行下一层递归 $formatTree = array_merge($formatTree, $middle); } } return $formatTree; } if (!function_exists('array_column')) { function array_column($array, $val, $key = null){ $newArr = array(); if( is_null($key) ){ foreach ($array as $index => $item) { $newArr[] = $item[$val]; } }else{ foreach ($array as $index => $item) { $newArr[$item[$key]] = $item[$val]; } } return $newArr; } }