B
B
BonBon Slick2016-10-21 14:38:52
PHP
BonBon Slick, 2016-10-21 14:38:52

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);

I'm thinking of doing it like in WordPress , when creating a category, specify its parent, and in the database you need to somehow organize this. How best to do this, who will tell you?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-10-21
@BonBonSlick

For trees with unknown nesting, the nestedsets approach is more justified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question