php 递归树形结构

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;
}

 

posted @ 2018-07-06 16:19  不一样的开始  阅读(413)  评论(0编辑  收藏  举报