PHP 无限极分类

1.循环迭代实现无限极分类

 public function test(){
        // 数组下标与id 一致
        $depart_list = [
            1 => ['id' => 1, 'grp_name' => '技术部', 'pid' => 0],
            2 => ['id' => 2, 'grp_name' => '业务中台', 'pid' => 1],
            3 => ['id' => 3, 'grp_name' => '业务前台', 'pid' => 1],
            4 => ['id' => 4, 'grp_name' => '业务后台', 'pid' => 1],
            5 => ['id' => 5, 'grp_name' => '客服组', 'pid' => 2],
            6 => ['id' => 6, 'grp_name' => '管理系统组', 'pid' => 2],
            7 => ['id' => 7, 'grp_name' => '支付组', 'pid' => 2],
            8 => ['id' => 8, 'grp_name' => '用户组', 'pid' => 4],
            9 => ['id' => 9, 'grp_name' => '司机组', 'pid' => 4],
            10 => ['id' => 10, 'grp_name' => 'ios组', 'pid' => 3],
            11 => ['id' => 11, 'grp_name' => 'android组', 'pid' => 3],
        ];
        $depart_data = $this->generateTree($depart_list);
        print_r(json_encode($depart_data, JSON_UNESCAPED_UNICODE));
    }

  function generateTree($items){
        $tree = array();
        foreach($items as $item){
            if(isset($items[$item['pid']])){
                $items[$item['pid']]['son'][] = &$items[$item['id']];
            }else{
                $tree[] = &$items[$item['id']];
            }
        }
        return $tree;
    }

输出结果如下:

[
    {
        "id": 1,
        "grp_name": "技术部",
        "pid": 0,
        "son": [
            {
                "id": 2,
                "grp_name": "业务中台",
                "pid": 1,
                "son": [
                    {
                        "id": 5,
                        "grp_name": "客服组",
                        "pid": 2
                    },
                    {
                        "id": 6,
                        "grp_name": "管理系统组",
                        "pid": 2
                    },
                    {
                        "id": 7,
                        "grp_name": "支付组",
                        "pid": 2
                    }
                ]
            },
            {
                "id": 3,
                "grp_name": "业务前台",
                "pid": 1,
                "son": [
                    {
                        "id": 10,
                        "grp_name": "ios组",
                        "pid": 3
                    },
                    {
                        "id": 11,
                        "grp_name": "android组",
                        "pid": 3
                    }
                ]
            },
            {
                "id": 4,
                "grp_name": "业务后台",
                "pid": 1,
                "son": [
                    {
                        "id": 8,
                        "grp_name": "用户组",
                        "pid": 4
                    },
                    {
                        "id": 9,
                        "grp_name": "司机组",
                        "pid": 4
                    }
                ]
            }
        ]
    }
]

分析:
这个算法利用了循环迭代,将线性结构按照父子关系以树形结构输出,算法的关键在于使用了引用.
优点:速度快,效率高.
缺点:数组的key值必须与id值相同,不便于取出数据(同样使用迭代获取数据)

2.递归实现无限极分类:

 public function test(){
        $depart_list = [
            // 数组下标与id  可以不一致
            0 => ['id' => 1, 'grp_name' => '技术部', 'pid' => 0],
            1 => ['id' => 2, 'grp_name' => '业务中台', 'pid' => 1],
            2 => ['id' => 3, 'grp_name' => '业务前台', 'pid' => 1],
            3 => ['id' => 4, 'grp_name' => '业务后台', 'pid' => 1],
            4 => ['id' => 5, 'grp_name' => '客服组', 'pid' => 2],
            5 => ['id' => 6, 'grp_name' => '管理系统组', 'pid' => 2],
            6 => ['id' => 7, 'grp_name' => '支付组', 'pid' => 2],
            7 => ['id' => 8, 'grp_name' => '用户组', 'pid' => 4],
            8 => ['id' => 9, 'grp_name' => '司机组', 'pid' => 4],
            9 => ['id' => 10, 'grp_name' => 'ios组', 'pid' => 3],
            10 => ['id' => 11, 'grp_name' => 'android组', 'pid' => 3],
        ];
        $depart_data = $this->generateTree($depart_list, 0, 0);
        foreach ($depart_data as $val){
            echo $val['grp_name'].'
'; } } public function generateTree(array $items, $id, $step){ static $tree=[]; foreach($items as $key=>$val) { if($val['pid'] == $id) { $flg = str_repeat('―',$step*2); $val['grp_name'] = $flg.$val['grp_name']; $tree[] = $val; $this->generateTree($items , $val['id'] ,$step+1); } } return $tree; }

输出如下:


BFC397D8-2535-4C62-9708-9BA121941774.png

分析:
利用了递归,数组的key值与id值可以不相同,最后以顺序的结构输出数组
优点:方便遍历,查找父子元素
缺点:php不擅长递归,数据量大的情况下效率会显著降低

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