哪位大师能用匿名递归下这个无限极分类?

需求:
1、给下面的每个ID,添加一个“authority”属性,
2、authority属性 是一个字符串或者是一个数组字符串,样式如下:”authority”:[“admin”]或者:”authority”:[“admin”,”user”,”guest”]。

3、请用匿名函数递归的方式把authority属性,分别插入每个ID项。

{
“data”: [
{
“id”: 1,
“parent_id”: 0,
“parent_path”: null,
“name”: “欢迎”,
“icon”: “smile”,
“path”: “/welcome”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: [
{
“id”: 2,
“parent_id”: 1,
“parent_path”: null,
“name”: “表单”,
“icon”: “form”,
“path”: “/form”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: []
},
{
“id”: 3,
“parent_id”: 1,
“parent_path”: null,
“name”: “基础表单”,
“icon”: “smile”,
“path”: “/form/basic-form”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: []
}
]
},
{
“id”: 4,
“parent_id”: 0,
“parent_path”: null,
“name”: “列表”,
“icon”: “table”,
“path”: “/list”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: [
{
“id”: 5,
“parent_id”: 4,
“parent_path”: null,
“name”: “查询列表”,
“icon”: “smile”,
“path”: “/list/search”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: []
},
{
“id”: 6,
“parent_id”: 4,
“parent_path”: null,
“name”: “项目列表”,
“icon”: “smile”,
“path”: “/list/search/projects”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: []
}
]
},
{
“id”: 7,
“parent_id”: 0,
“parent_path”: null,
“name”: “信息”,
“icon”: “profile”,
“path”: “/profile”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: [
{
“id”: 8,
“parent_id”: 7,
“parent_path”: null,
“name”: “基本信息”,
“icon”: “profile”,
“path”: “/profile/basic”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: []
},
{
“id”: 9,
“parent_id”: 7,
“parent_path”: null,
“name”: “个性信息”,
“icon”: “profile”,
“path”: “/profile/advanced”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: []
}
]
},
{
“id”: 10,
“parent_id”: 0,
“parent_path”: null,
“name”: “调试”,
“icon”: “bug”,
“path”: “/result”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: [
{
“id”: 11,
“parent_id”: 10,
“parent_path”: null,
“name”: “结果集”,
“icon”: “CheckCircleOutlined”,
“path”: “/result/success’”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: []
},
{
“id”: 12,
“parent_id”: 10,
“parent_path”: null,
“name”: “错误表”,
“icon”: “warning”,
“path”: “/exception”,
“locale”: null,
“sequence”: 10000,
“hideInMenu”: 0,
“hideChildrenInMenu”: 0,
“children”: []
}
]
}
]
}

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 2
自由与温暖是遥不可及的梦想
3年前 评论
自由与温暖是遥不可及的梦想 (作者) 3年前
dongzhiyu (楼主) 3年前
自由与温暖是遥不可及的梦想 (作者) 3年前
dongzhiyu (楼主) 3年前
dongzhiyu (楼主) 3年前
dongzhiyu (楼主) 3年前

1.先定义一个简单的结构一致的模拟数据:

$demo = [
    [
        'id' => 1,
        'name' => 'simple',
        'children' => [
            'id' => 2,
            'name' => 'code',
        ],
    ],
];

2.再定义要插入的数据:

$insertArr = ['authority'=> ['big','large']];

3.抽象分解这个过程要处理的逻辑

把逻辑和具体的数据抽离,这里体验逻辑

function simpleCode($origin, $nodeName, $insertArr)
{
    // 逻辑1.插入操作
    $insertFuc = function(&$originArr) use ($insertArr) {
        foreach ($insertArr as $key =>$value) {
            $originArr[$key] = $value;
        }
    };
     // 逻辑2.插入操作调用者
    $loopFuc = function (&$originArr) use ($nodeName, &$loopFuc, &$insertFuc) {
        if (array_key_exists($nodeName, $originArr) && count($originArr[$nodeName]) > 0) {
            $loopFuc($originArr[$nodeName]);
        }
        $insertFuc($originArr);
    };
    // 最后组装:
    foreach ($origin as &$value) {
        $loopFuc($value);
    }
    return $origin;
}

4:测试代码:

$data = simpleCode($demo, 'children', $insertArr);
print_r($data);

结果:

file

5:全部代码:

<?php

$demo = [
    [
        'id' => 1,
        'name' => 'simple',
        'children' => [
            'id' => 2,
            'name' => 'code',
        ],
    ],
];

$insertArr = ['authority' => ['big', 'large']];

function simpleCode($origin, $nodeName, $insertArr)
{
    $insertFuc = function (&$originArr) use ($insertArr) {
        foreach ($insertArr as $key => $value) {
            $originArr[$key] = $value;
        }
    };

    $loopFuc = function (&$originArr) use ($nodeName, &$loopFuc, &$insertFuc) {
        if (
            array_key_exists($nodeName, $originArr)
            && count($originArr[$nodeName]) > 0
        ) {
            $loopFuc($originArr[$nodeName]);
        }
        $insertFuc($originArr);
    };

    foreach ($origin as &$value) {
        $loopFuc($value);
    }
    return $origin;
}

$data = simpleCode($demo, 'children', $insertArr);
print_r($data);
3年前 评论
dongzhiyu (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!