Answer the question
In order to leave comments, you need to log in
How to link dynamic HTML arrays to each other, at a specific index?
There is a task, display a dynamic menu, it contains sub, sub-sub .... all menus are nested. (instead of menus, you can imagine that these are categories, and all their subsequent sub-sub-subs ...).
Here is a recursive php script that is responsible for outputting everything:
$menus = [
'menu_1' => [
'sub_menu_1_1',
'sub_menu_1_2'=>['sub_sub_menu_1_2_1'=>['sub_sub_sub_menu_1_2_1_1'],],
],
'menu_2' => [
'sub_menu_2_2' => ['sub_sub_menu_2_1','sub_sub_menu_2_2',],
],
];
function get_menus( array $menus ) {
foreach ( $menus as $menu => $submenu) {
if ( is_array( $submenu ) ) {
echo "$menu <br/>";
get_menus( $submenu );
} else {
echo "$submenu<br/>";
}
}
}
get_menus($menus);
Answer the question
In order to leave comments, you need to log in
For trees with unknown nesting, the nestedsets approach is more justified.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question