我具有以下数组结构:
Array ( [0] => Array ( [id] => 10 [parent] => 0 [username] => PDV [children] => Array ( [0] => Array ( [id] => 100 [parent] => 10 [username] => Terminal de Vendas ) [1] => Array ( [id] => 101 [parent] => 10 [username] => Clientes ) [2] => Array ( [id] => 102 [parent] => 10 [username] => Fechamento ) [3] => Array ( [id] => 103 [parent] => 10 [username] => Relatórios de Venda [children] => Array ( [0] => Array ( [id] => 1004 [parent] => 103 [username] => Relatório de Venda 1 ) [1] => Array ( [id] => 1005 [parent] => 103 [username] => Relatório de Venda 2 ) ) ) ) ) [1] => Array ( [id] => 11 [parent] => 0 [username] => Estoque [children] => Array ( [0] => Array ( [id] => 111 [parent] => 11 [username] => Cadastro de produtos ) [1] => Array ( [id] => 112 [parent] => 11 [username] => Entrada Produtos ) [2] => Array ( [id] => 113 [parent] => 11 [username] => Relatórios de Estoque [children] => Array ( [0] => Array ( [id] => 1104 [parent] => 113 [username] => Relatório de Estoque A ) [1] => Array ( [id] => 1105 [parent] => 113 [username] => Relatório de Estoque B ) ) ) ) ) [2] => Array ( [id] => 13 [parent] => 0 [username] => Financeiro [children] => Array ( [0] => Array ( [id] => 126 [parent] => 13 [username] => Notas de Entrada ) [1] => Array ( [id] => 127 [parent] => 13 [username] => Notas de Saída ) [2] => Array ( [id] => 128 [parent] => 13 [username] => Vendas Diárias ) [3] => Array ( [id] => 129 [parent] => 13 [username] => Balanço ) [4] => Array ( [id] => 130 [parent] => 13 [username] => Relatórios [children] => Array ( [0] => Array ( [id] => 1301 [parent] => 130 [username] => Relatório 1 ) ) ) ) ) [3] => Array ( [id] => 12 [parent] => 0 [username] => Sistema [children] => Array ( [0] => Array ( [id] => 107 [parent] => 12 [username] => Ajuda... [children] => Array ( [0] => Array ( [id] => 1008 [parent] => 107 [username] => Conteúdo... ) [1] => Array ( [id] => 1009 [parent] => 107 [username] => Licença ) [2] => Array ( [id] => 1100 [parent] => 107 [username] => Sobre... ) ) ) [1] => Array ( [id] => 118 [parent] => 12 [username] => Usuários [children] => Array ( [0] => Array ( [id] => 1200 [parent] => 118 [username] => Cadastro ) [1] => Array ( [id] => 1303 [parent] => 118 [username] => Grupos ) [2] => Array ( [id] => 1201 [parent] => 118 [username] => Permissões ) [3] => Array ( [id] => 1202 [parent] => 118 [username] => Minha Conta [children] => Array ( [0] => Array ( [id] => 13002 [parent] => 1202 [username] => Dados Pessoais [children] => Array ( [0] => Array ( [id] => 130002 [parent] => 12002 [username] => TESTE 1 ) [1] => Array ( [id] => 130003 [parent] => 12002 [username] => TESTE 2 ) ) ) [1] => Array ( [id] => 12003 [parent] => 1202 [username] => Alterar Senha [children] => Array ( [0] => Array ( [id] => 130004 [parent] => 12003 [username] => TESTE 3 ) ) ) [2] => Array ( [id] => 1204 [parent] => 1202 [username] => Sair ) ) ) ) ) [2] => Array ( [id] => 125 [parent] => 12 [username] => Preferências ) [3] => Array ( [id] => 119 [parent] => 12 [username] => Configuração ) ) ) )
我已经工作了几个小时,我的头都开裂了。拜托,任何人都可以告诉我如何限制孩子们返回的深度吗?
我想参与这个结构。例如:对于基于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;
$ result:
Array ( [0] => Array ( [id] => 118 [parent] => 12 [username] => Usuários [children] => Array ( [0] => Array ( [id] => 1200 [parent] => 118 [username] => Cadastro ) [1] => Array ( [id] => 1303 [parent] => 118 [username] => Grupos ) [2] => Array ( [id] => 1201 [parent] => 118 [username] => Permissões ) [3] => Array ( [id] => 1202 [parent] => 118 [username] => Minha Conta [children] => true ) ) ) )
功能:
参数:
$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;
function TreePartByID($ID, $parents=0, $childrens=1){ ... return $arrayResult; }
TreePartByID(118)结果:o路径完成引用了项目118
Array ( [0] => Array ( [id] => 12 [parent] => 0 [username] => Sistema [children] => Array ( [0] => Array ( [id] => 118 [parent] => 12 [username] => Usuários [children] => Array ( [0] => Array ( [id] => 1200 [parent] => 118 [username] => Cadastro ) [1] => Array ( [id] => 1303 [parent] => 118 [username] => Grupos ) [2] => Array ( [id] => 1201 [parent] => 118 [username] => Permissões ) [3] => Array ( [id] => 1202 [parent] => 118 [username] => Minha Conta [children] => Array ( [0] => Array ( [id] => 13002 [parent] => 1202 [username] => Dados Pessoais [children] => Array ( [0] => Array ( [id] => 130002 [parent] => 12002 [username] => TESTE 1 ) [1] => Array ( [id] => 130003 [parent] => 12002 [username] => TESTE 2 ) ) ) [1] => Array ( [id] => 12003 [parent] => 1202 [username] => Alterar Senha [children] => Array ( [0] => Array ( [id] => 130004 [parent] => 12003 [username] => TESTE ) ) ) [2] => Array ( [id] => 1204 [parent] => 1202 [username] => Sair ) ) ) ) ) ) ) )
TreePartByID(1202,1,1); // retorna 1 item como parent(118),e filhos imediatos do ID 1202(1nível);
Array ( [0] => Array ( [id] => 118 [parent] => 12 [username] => Usuários [children] => Array ( [0] => Array ( [id] => 1202 [parent] => 118 [username] => Minha Conta [children] => Array ( [0] => Array ( [id] => 13002 [parent] => 1202 [username] => Dados Pessoais [children] => true ) [1] => Array ( [id] => 12003 [parent] => 1202 [username] => Alterar Senha [children] => true ) [2] => Array ( [id] => 1204 [parent] => 1202 [username] => Sair ) ) ) ) ) )