PHP数组列表转树形结构

 

 

今天突然想用到这个功能  结果百度到的  基本是写乱糟糟的一堆代码  无奈只好亲自操刀

话不多说,先上代码:

class Tools{


    public static function listToTree($data,$pid = 0){
        $arr = array_filter($data,function ($v) use ($pid){
            if($pid == $v['pid']){
                return true;
            }
            return false;
        });
        $arr = array_values($arr);
        $tools = __CLASS__ ;
        $res = array_map(function ($v) use ($data,$tools){
            $v['children'] = $tools::listToTree($data,$v['id']);
            return $v;
        },$arr);
        return $res;

    }
}

  

测试数据如下:

$list = [
            {
                "id": 1,
                "notice": "订单管理",
                "level": 1,
                "pid": 0,
                "path": "-1"
            },
            {
                "id": 2,
                "notice": "产品管理",
                "level": 1,
                "pid": 0,
                "path": "-2"
            },
            {
                "id": 3,
                "notice": "权限管理",
                "level": 1,
                "pid": 0,
                "path": "-3"
            },
            {
                "id": 4,
                "notice": "订单列表",
                "level": 2,
                "pid": 1,
                "path": "-1-4"
            },
            {
                "id": 5,
                "notice": "退款管理",
                "level": 2,
                "pid": 1,
                "path": "-1-5"
            },
            {
                "id": 6,
                "notice": "产品列表",
                "level": 2,
                "pid": 2,
                "path": "-2-6"
            },
            {
                "id": 7,
                "notice": "产品分类",
                "level": 2,
                "pid": 2,
                "path": "-2-7"
            },
            {
                "id": 8,
                "notice": "用户管理",
                "level": 2,
                "pid": 3,
                "path": "-3-8"
            },
            {
                "id": 9,
                "notice": "角色管理",
                "level": 2,
                "pid": 3,
                "path": "-3-9"
            },
            {
                "id": 10,
                "notice": "菜单管理",
                "level": 2,
                "pid": 3,
                "path": "-3-10"
            }
        ];
var_dump(Tools::listToTree($list));

  

结果:

 [
            {
                "id": 1,
                "notice": "订单管理",
                "level": 1,
                "pid": 0,
                "path": "-1",
                "children": [
                    {
                        "id": 4,
                        "notice": "订单列表",
                        "level": 2,
                        "pid": 1,
                        "path": "-1-4",
                        "children": []
                    },
                    {
                        "id": 5,
                        "notice": "退款管理",
                        "level": 2,
                        "pid": 1,
                        "path": "-1-5",
                        "children": []
                    }
                ]
            },
            {
                "id": 2,
                "notice": "产品管理",
                "level": 1,
                "pid": 0,
                "path": "-2",
                "children": [
                    {
                        "id": 6,
                        "notice": "产品列表",
                        "level": 2,
                        "pid": 2,
                        "path": "-2-6",
                        "children": []
                    },
                    {
                        "id": 7,
                        "notice": "产品分类",
                        "level": 2,
                        "pid": 2,
                        "path": "-2-7",
                        "children": []
                    }
                ]
            },
            {
                "id": 3,
                "notice": "权限管理",
                "level": 1,
                "pid": 0,
                "path": "-3",
                "children": [
                    {
                        "id": 8,
                        "notice": "用户管理",
                        "level": 2,
                        "pid": 3,
                        "path": "-3-8",
                        "children": []
                    },
                    {
                        "id": 9,
                        "notice": "角色管理",
                        "level": 2,
                        "pid": 3,
                        "path": "-3-9",
                        "children": []
                    },
                    {
                        "id": 10,
                        "notice": "菜单管理",
                        "level": 2,
                        "pid": 3,
                        "path": "-3-10",
                        "children": []
                    }
                ]
            }
        ]
原文地址:https://www.cnblogs.com/wh-alan/p/15529105.html