php无限极分类

/*
 *p1 数据源
 *p2 类型$type=1返回一个二维数组,$type=2返回子孙树
 *p3 初始父id
 *p4 分类等级
 */
function unlimitTree($data,$type,$pid=0,$level=1)
    {
        if($type == 1){
            static $tree = [];
        }else{
            $tree = [];
        }

        foreach($data as $key => $value) {
            if($value['pid'] == $pid) {
                if($type == 1){
                    $value['level'] = $level;
                    $tree[] = $value;
                    unlimitTree($data,$type,$value['id'],$level+1);
                }else{
                    $value['level'] = $level;
                    $value['children'] = unlimitTree($data,$type,$value['id'],$level+1);
                    $tree[$value['id']] = $value;
                }
            }
        }
        return $tree;
    }

你可能感兴趣的:(php无限极分类)