Create Infinite PHP Menu

2

I am trying to create an infinite menu, I already mounted the object the way I need, but I can not print. It should look 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 output:

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 >
    
asked by anonymous 05.04.2018 / 21:07

1 answer

2

Try this:

public function menuList($lists, $n = "") {
    if(!is_array($lists)){
        $lists = $lists->toArray();
    }
    for($x = 0; $x < count($lists); $x++){
        $element = $lists[$x];
       echo $n.$element->nome . "<br>";
        if($element->children){
            $n .= $element->nome . " > ";
            $this->menuList($element->children, $n);
            $count = strlen($element->nome . " > ");
            $n = substr($n, 0, -$count);
        } 
    }
 }
    
06.04.2018 / 03:09