• 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": []
                        }
                    ]
                }
            ]
  • 相关阅读:
    02、Rendering a Triangle
    [转]Unity性能优化之Draw Call
    [转]Directx11 3D空间坐标系认识
    设置让EditPlus不产生BAK文件
    深度优先搜索与广度优先搜索对比
    python多重继承新算法C3
    php的垃圾回收机制
    python脚本自动发邮件功能
    python的keyword模块
    EditPlus如何设置——自动换行
  • 原文地址:https://www.cnblogs.com/wh-alan/p/15529105.html
Copyright © 2020-2023  润新知