首页
学习
活动
专区
圈层
工具
发布

php数组通过递归转换成无限级树结构

代码语言:php
复制
   //id作为索引,pid 为父索引
   function tree(&$list,$pid=0){
        $tree=[];
        foreach ($list as $key=>$item){
            if ($item['pid']===$pid){
                $tree[$item['id']]=$item;
                unset($list[key]); //删除当前项,减小递归压力
                $tree[$item['id']]['children']=tree($list,$item['id']); //使用children 接收递归出的子数组
            }
        }
        return $tree;
    }
下一篇
举报
领券