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 );
return $build;before the closing}of the function?return $build;before the closing}of the function. But also u should concat returned value inside the function - need to change$traverse( $menu->children,$build);to$build .= $traverse( $menu->children,$build);