Thursday, 11 October 2018

how i can return a value after end recursive function in php

I need after the end of a loop in a recursive function to return the $build variable

This is my code:

    $traverse = function ($tree,$build = '') use (&$traverse) {

        foreach ($tree as $key=>$menu) {
            if (count($menu->children) > 0) {
                $build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a><ul>";
                    $traverse( $menu->children,$build);
                $build .= "</ul></li>";
            } else {
                $build .= "<li ><a href='" . $menu->url . "'>" . $menu->text . "</a></li>";
            }
        }
    };


 $traverse($tree );



from how i can return a value after end recursive function in php

No comments:

Post a Comment