tp框架中,前端页面经常会用到无限级分类列表  和  父子级树状列表

  //递归函数 实现无限级分类列表
    function get_cate_list($list,$pid=0,$level=0) {
        static $tree = array();
        foreach($list as $row) {
            if($row['pid']==$pid) {
                $row['level'] = $level;
                $tree[] = $row;
                get_cate_list($list, $row['id'], $level + 1);
            }
        }
        return $tree;
    }
//引用方式实现 父子级树状结构
    function get_tree_list($list){
        //将每条数据中的id值作为其下标
        $temp = [];
        foreach($list as $v){
            $v['son'] = [];
            $temp[$v['id']] = $v;
        }
        //获取分类树
        foreach($temp as $k=>$v){
            $temp[$v['pid']]['son'][] = &$temp[$v['id']];
        }
        return isset($temp[0]['son']) ? $temp[0]['son'] : [];
    }

$list 需要是标准的二维数组;

相关文章:

  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
  • 2021-06-25
  • 2021-06-28
  • 2022-01-06
猜你喜欢
  • 2022-12-23
  • 2022-01-12
  • 2021-08-29
  • 2021-07-25
  • 2022-12-23
相关资源
相似解决方案