递归展示多级菜单

public function getChlidPresona ($info, $presona_pid=0) {
        //递归显示机构组信息
        $list = [];
        foreach($info as $k => $v) {
            if($v['presona_pid'] == $presona_pid) {
                $arr = array();
                $arr['id'] = $v['id'];
                $arr['presona'] = $v['presona'];
                $arr['presona_pid'] = $v['presona_pid'];
                $arr['presona_user_name'] = $v['user_name'];
                $arr['presona_num'] = $v['presona_num'];
                $arr['remarks'] = $v['remarks'];
                $list[$k] = $arr;
                unset($info[$k]);
                $list[$k]['childList'] = $this->getChlidPresona($info, $v['id']);
            }
        }

        $tree = array_values($list);
        return $tree;
    }

 

public function getTree($arr,$pid=0,$level=1){
        static $tree = [];
        foreach ($arr as $key => $value) {
            $pre = $level > 1 ? "|" : "";
            if($value['pid'] == $pid){
                for($i=1;$i<$level;$i++){
                    $pre .= '--';
                }
                $value['pre'] = $pre;
                $value['level'] = $level;
                $tree[] = $value;
                unset($arr[$key]); // 删除已排好的数据,减少循环次数
                $this->getTree($arr,$value['id'],$level+1);
            }
        }
        return $tree;
    }

 

posted @ 2023-03-02 13:53  流浪2024  阅读(28)  评论(0编辑  收藏  举报