zoukankan      html  css  js  c++  java
  • 递归部门

    部门有子部门,子部门又有子部分,后代部门无限制,数据库储存结构如datalist,通常需要构造成层次结构。

    <?php
    
    $deptlist = [
        ['id'=>'1','name'=>'名称1','pid'=>'0'],
        ['id'=>'2','name'=>'名称2','pid'=>'1'],
        ['id'=>'3','name'=>'名称3','pid'=>'1'],
        ['id'=>'4','name'=>'名称4','pid'=>'3'],
        ['id'=>'5','name'=>'名称5','pid'=>'3'],
        ['id'=>'6','name'=>'名称6','pid'=>'5'],
        ['id'=>'7','name'=>'名称7','pid'=>'5'],
        ['id'=>'8','name'=>'名称8','pid'=>'0'],
        ['id'=>'9','name'=>'名称9','pid'=>'8'],
        ['id'=>'10','name'=>'名称10','pid'=>'8'],
    ];
    
    
    function getDeptbyPid($arr,$pid){
        $filtered = array_filter($arr, function($item) use ($pid) {
            return $item['pid'] == $pid;
        });
        return $filtered;
    
    }
    
    $topdepts = getDeptbyPid($deptlist,'0');
    
    foreach ($topdepts as &$v){
        $v['son'] = getSonDept($deptlist,$v['id']);
    }
    
    
    function getSonDept($arr,$pid){
        $dpets = getDeptbyPid($arr,$pid);
        foreach ($dpets as &$v){
            $v['son'] = getSonDept($arr,$v['id']);
        }
    
        return $dpets;
    }
    
    print_r($topdepts);

    结果

    {
        "0": {
            "id": "1",
            "name": "名称1",
            "pid": "0",
            "son": {
                "1": {
                    "id": "2",
                    "name": "名称2",
                    "pid": "1",
                    "son": []
                },
                "2": {
                    "id": "3",
                    "name": "名称3",
                    "pid": "1",
                    "son": {
                        "3": {
                            "id": "4",
                            "name": "名称4",
                            "pid": "3",
                            "son": []
                        },
                        "4": {
                            "id": "5",
                            "name": "名称5",
                            "pid": "3",
                            "son": {
                                "5": {
                                    "id": "6",
                                    "name": "名称6",
                                    "pid": "5",
                                    "son": []
                                },
                                "6": {
                                    "id": "7",
                                    "name": "名称7",
                                    "pid": "5",
                                    "son": []
                                }
                            }
                        }
                    }
                }
            }
        },
        "7": {
            "id": "8",
            "name": "名称8",
            "pid": "0",
            "son": {
                "8": {
                    "id": "9",
                    "name": "名称9",
                    "pid": "8",
                    "son": []
                },
                "9": {
                    "id": "10",
                    "name": "名称10",
                    "pid": "8",
                    "son": []
                }
            }
        }
    }

    其中

    $topdepts = getDeptbyPid($deptlist,'0');
    
    foreach ($topdepts as &$v){
        $v['son'] = getSonDept($deptlist,$v['id']);
    }

    可以简化为

    getSonDept($deptlist,'0')
  • 相关阅读:
    search方法的使用
    边界字符的使用
    重复数量限定符
    常用匹配符
    使用JS快速读取TXT文件
    基于jq和纯js的 读取本地.txt文件的方法
    Linux中的du和df命令
    HSSFWorkbook
    el表达式
    eclipse 导入web项目时常见错误
  • 原文地址:https://www.cnblogs.com/jimzbom/p/7790368.html
Copyright © 2011-2022 走看看