function tree($arr, $pid=0, $level=0){ static $tree = []; foreach($arr as $v){ if($v['pid'] == $pid){ $v['level'] = $level; if($pid == 0){ $v['name'] = $v['name']; }else{ $v['name'] = str_repeat(' ', $level).'└─'.$v['name']; } $tree[]=$v; tree($arr, $v['id'], $level+1); } } return $tree; }