У меня следующая структура массива:
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
)
)
)
)
Я работаю часами, и у меня ломается голова.Пожалуйста, кто-нибудь может подсказать, как ограничить глубину возвращения детей?
Я хочу получить часть этой структуры.Например: на основании идентификатора 118, не возвращайте родителей и 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;
$ Результат:
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 (1 ní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
)
)
)
)
)
)