php - 如何限制孩子在PHP中返回的深度
问题描述
我有以下数组结构:
大批 ( [0] => 数组 ( [id] => 10 [父] => 0 [用户名] => PDV [孩子] => 数组 ( [0] => 数组 ( [id] => 100 [父] => 10 [用户名] => Terminal de Vendas ) [1] => 数组 ( [id] => 101 [父] => 10 [用户名] => 客户 ) [2] => 数组 ( [id] => 102 [父] => 10 [用户名] => Fechamento ) [3] => 数组 ( [id] => 103 [父] => 10 [用户名] => Relatórios de Venda [孩子] => 数组 ( [0] => 数组 ( [id] => 1004 [父] => 103 [用户名] => Relatório de Venda 1 ) [1] => 数组 ( [id] => 1005 [父] => 103 [用户名] => Relatório de Venda 2 ) ) ) ) ) [1] => 数组 ( [id] => 11 [父] => 0 [用户名] => Estoque [孩子] => 数组 ( [0] => 数组 ( [id] => 111 [父] => 11 [用户名] => 产品地籍 ) [1] => 数组 ( [id] => 112 [父] => 11 [用户名] => Entrada 产品 ) [2] => 数组 ( [id] => 113 [父] => 11 [用户名] => Relatórios de Estoque [孩子] => 数组 ( [0] => 数组 ( [id] => 1104 [父] => 113 [用户名] => Relatório de Estoque A ) [1] => 数组 ( [id] => 1105 [父] => 113 [用户名] => Relatório de Estoque B ) ) ) ) ) [2] => 数组 ( [id] => 13 [父] => 0 [用户名] => Financeiro [孩子] => 数组 ( [0] => 数组 ( [id] => 126 [父] => 13 [用户名] => Notas de Entrada ) [1] => 数组 ( [id] => 127 [父] => 13 [用户名] => Notas de Saída ) [2] => 数组 ( [id] => 128 [父] => 13 [用户名] => 文达斯日记 ) [3] => 数组 ( [id] => 129 [父] => 13 [用户名] => Balanço ) [4] => 数组 ( [id] => 130 [父] => 13 [用户名] => Relatórios [孩子] => 数组 ( [0] => 数组 ( [id] => 1301 [父] => 130 [用户名] => Relatório 1 ) ) ) ) ) [3] => 数组 ( [id] => 12 [父] => 0 [用户名] => Sistema [孩子] => 数组 ( [0] => 数组 ( [id] => 107 [父] => 12 [用户名] => Ajuda... [孩子] => 数组 ( [0] => 数组 ( [id] => 1008 [父] => 107 [用户名] => Conteúdo... ) [1] => 数组 ( [id] => 1009 [父] => 107 [用户名] => 许可证 ) [2] => 数组 ( [id] => 1100 [父] => 107 [用户名] => 清醒... ) ) ) [1] => 数组 ( [id] => 118 [父] => 12 [用户名] => Usuários [孩子] => 数组 ( [0] => 数组 ( [id] => 1200 [父] => 118 [用户名] => 地籍 ) [1] => 数组 ( [id] => 1303 [父] => 118 [用户名] => Grupos ) [2] => 数组 ( [id] => 1201 [父] => 118 [用户名] => Permissões ) [3] => 数组 ( [id] => 1202 [父] => 118 [用户名] => Minha Conta [孩子] => 数组 ( [0] => 数组 ( [id] => 13002 [父] => 1202 [用户名] => Dados Pessoais [孩子] => 数组 ( [0] => 数组 ( [id] => 130002 [父] => 12002 [用户名] => 测试 1 ) [1] => 数组 ( [id] => 130003 [父] => 12002 [用户名] => 测试 2 ) ) ) [1] => 数组 ( [id] => 12003 [父] => 1202 [用户名] => Alterar Senha [孩子] => 数组 ( [0] => 数组 ( [id] => 130004 [父] => 12003 [用户名] => 测试 3 ) ) ) [2] => 数组 ( [id] => 1204 [父] => 1202 [用户名] => Sair ) ) ) ) ) [2] => 数组 ( [id] => 125 [父] => 12 [用户名] => Preferências ) [3] => 数组 ( [id] => 119 [父] => 12 [用户名] => 配置 ) ) ) )
我已经工作了好几个小时,我的头都快碎了。拜托,谁能告诉我如何限制孩子返回的深度?
我想得到这个结构的一部分。例如:以 ID 118 为基础,从该 ID 中返回没有父母和 1 级子级。他们调用一个函数:
$result = TreePartByID($source, 118, null, 1);
$source Is the full array structure;
118 Is the ID to use as item base;
null Is the number of parents to return from the ID 118. In this case, null represents the some iten with ID 118;
1 Is a depth of childrens to return from ID 118. This case is only immediate childrens relative to ID 118;
$结果:
大批 ( [0] => 数组 ( [id] => 118 [父] => 12 [用户名] => Usuários [孩子] => 数组 ( [0] => 数组 ( [id] => 1200 [父] => 118 [用户名] => 地籍 ) [1] => 数组 ( [id] => 1303 [父] => 118 [用户名] => Grupos ) [2] => 数组 ( [id] => 1201 [父] => 118 [用户名] => Permissões ) [3] => 数组 ( [id] => 1202 [父] => 118 [用户名] => Minha Conta [孩子] => 真的 ) ) ) )
功能:
参数:
$ID = ID of iten defined as default;
$parents = Number of parents from the $ID. If null, retur the own $ID, else if 0(zero) return from the root and any number is the number of parents;
$childrens = Number of childrens from the $ID. if 0(zero) no return childrens;
函数 TreePartByID($ID, $parents=0, $childrens=1){ ... 返回 $arrayResult; }
TreePartByID(118) 结果:o 完成引用 ao 项目 118 的路径
大批 ( [0] => 数组 ( [id] => 12 [父] => 0 [用户名] => Sistema [孩子] => 数组 ( [0] => 数组 ( [id] => 118 [父] => 12 [用户名] => Usuários [孩子] => 数组 ( [0] => 数组 ( [id] => 1200 [父] => 118 [用户名] => 地籍 ) [1] => 数组 ( [id] => 1303 [父] => 118 [用户名] => Grupos ) [2] => 数组 ( [id] => 1201 [父] => 118 [用户名] => Permissões ) [3] => 数组 ( [id] => 1202 [父] => 118 [用户名] => Minha Conta [孩子] => 数组 ( [0] => 数组 ( [id] => 13002 [父] => 1202 [用户名] => Dados Pessoais [孩子] => 数组 ( [0] => 数组 ( [id] => 130002 [父] => 12002 [用户名] => 测试 1 ) [1] => 数组 ( [id] => 130003 [父] => 12002 [用户名] => 测试 2 ) ) ) [1] => 数组 ( [id] => 12003 [父] => 1202 [用户名] => Alterar Senha [孩子] => 数组 ( [0] => 数组 ( [id] => 130004 [父] => 12003 [用户名] => 测试 ) ) ) [2] => 数组 ( [id] => 1204 [父] => 1202 [用户名] => Sair ) ) ) ) ) ) ) )
TreePartByID(1202, 1, 1); // retorna 1 item como parent(118), e filhos imediatos do ID 1202 (1 nível);
大批 ( [0] => 数组 ( [id] => 118 [父] => 12 [用户名] => Usuários [孩子] => 数组 ( [0] => 数组 ( [id] => 1202 [父] => 118 [用户名] => Minha Conta [孩子] => 数组 ( [0] => 数组 ( [id] => 13002 [父] => 1202 [用户名] => Dados Pessoais [孩子] => 真的 ) [1] => 数组 ( [id] => 12003 [父] => 1202 [用户名] => Alterar Senha [孩子] => 真的 ) [2] => 数组 ( [id] => 1204 [父] => 1202 [用户名] => Sair ) ) ) ) ) )
解决方案
推荐阅读
- android - gmail 中忽略电子邮件意图主题,但可与其他邮件客户端一起使用
- javascript - 如何在验证弹出模式上放置要使用输入元素(v-select)固定的下拉项?
- python - 如何用scrapy在另一个方法中调用一个方法
- r - 创建第四行作为前 2 行之和的最快方法
- firefox - Greasemonkey 取消 Firefox 下载提示
- python - 日期时间库的值无效
- android - 自适应图标在奥利奥中不起作用 - 为什么?
- scikit-learn - sklearn.cross_validation 分数是什么意思?
- python - Python:当我使用seasonal_decompose时出错¶
- json - MongoDB 将文档字段拆分为两个字段