wesky

第一步,获取全部数据

第二部,写一个子分类的函数

第三,写一个递归循环函数

代码:

 /**
     * 查询所有数据  并且缓存
     * */
    public  function authData(){
        $res = $this->order(\'listorder asc\')->select();
        // 格式化
        foreach($res as $k=>$v){
            $result[$v[\'id\']] = $v;
        }
        Cache::set(\'auth\',$result);
        return $result;
    }
/**
     * 子类数组
     * */
    public function pidTree($pid=0){
        $result = \'\';
        $arr = $this->authData();
        if(empty($arr)) return false;
        foreach($arr as $k=>$v){
            if($v[\'pid\'] ==$pid){
                $result[$v[\'id\']] = $v;
            }
        }
        return $result;
    }

/**
     * 无限分类 树形结构
     * */
    public function getTree($pid=0){
        $arr = $this->pidTree($pid);
        if(empty($arr)) return false;
        foreach($arr as $k=>$v){
            $result[$v[\'id\']] = $v;
            $result[$v[\'id\']][\'son\'] = $this->getTree($v[\'id\']);

        }
        return $result;

    }

 

 

输出结果

 

分类:

技术点:

相关文章:

  • 2021-10-08
  • 2021-10-08
  • 2021-10-08
  • 2021-10-08
  • 2021-10-08
  • 2021-09-25
  • 2021-10-08
  • 2021-10-08
猜你喜欢
  • 2021-06-23
  • 2021-10-08
  • 2021-10-08
  • 2021-10-08
  • 2021-10-08
  • 2021-10-08
  • 2021-10-08
相关资源
相似解决方案