2
I’m trying to create an infinite menu, I’ve assembled the object the way I need to, but I can’t print it. It must be something like this:
User
User > User1
User > User2
User > User2 > User3
User > User2 > User4
User > User3
User > User3 > User5
.....
My Object:
[
(int) 0 => object(App\Model\Entity\User) {
'id' => (int) 1,
'nome' => 'Administrador',
'parent_id' => null,
'children' => [
(int) 0 => object(App\Model\Entity\User) {
'id' => (int) 2,
'nome' => 'Revenda Teste 1',
'parent_id' => (int) 1,
'children' => []
},
(int) 1 => object(App\Model\Entity\User) {
'id' => (int) 3,
'nome' => 'Revenda Teste 2',
'parent_id' => (int) 1,
'children' => []
},
(int) 2 => object(App\Model\Entity\User) {
'id' => (int) 6,
'nome' => 'Revenda Teste 3',
'parent_id' => (int) 1,
'children' => [],
},
(int) 3 => object(App\Model\Entity\User) {
'id' => (int) 7,
'nome' => 'Revenda Teste 4',
'parent_id' => (int) 1,
'children' => [
(int) 0 => object(App\Model\Entity\User) {
'id' => (int) 8,
'nome' => 'Revendedor Teste 5',
'parent_id' => (int) 7,
'children' => [
(int) 0 => object(App\Model\Entity\User) {
'id' => (int) 15,
'nome' => 'Revendedor do Rev 5',
'parent_id' => (int) 8,
'children' => [],
},
(int) 1 => object(App\Model\Entity\User) {
'id' => (int) 9,
'nome' => 'Revenda Teste 6',
'parent_id' => (int) 7,
'children' => [
(int) 0 => object(App\Model\Entity\User) {
'id' => (int) 14,
'nome' => 'Revenda Teste 7',
'parent_id' => (int) 9,
'children' => []
}
]
}
]
}
]
}
]
My code:
public function menuList($lists) {
$n = "";
if(!is_array($lists)){
$lists = $lists->toArray();
}
echo "<br>";
foreach($lists as $element) {
echo $n .= $element->nome . " > ";
if($element->children){
$this->menuList($element->children);
}
}
}
My way out:
Administrador >
Revenda Teste 1 > Revenda Teste 1 > Revenda Teste 2 > Revenda Teste 1 >
Revenda Teste 2 > Revenda Teste 3 > Revenda Teste 1 > Revenda Teste 2 >
Revenda Teste 3 > Revenda Teste 4 >
Revendedor Teste 5 >
Revendedor do Rev 5 > Revendedor Teste 5 > Revenda Teste 6 >
Revenda Teste 7 >
And how did you try to do it? You know what recursion is?
– Woss
yes, I tried with recursiveness, but I could not reach the intended result, I know that this is the way... but I do not know how to reach.
– Laercio Nunes
Ask the question your attempt and the result you got.
– Woss
ready... added the code and output.
– Laercio Nunes