php - From string to array to menu -
have list in db one:
id| val 1 | menu1 > submenu1 2 | menu1 > submenu1 > subsubmenu1 3 | menu1 > submenu1 > subsubmenu2 4 | menu1 > submenu1 > subsubmenu3 5 | menu1 > submenu1 > subsubmenu3 > lastmenu1 6 | menu2 > submenu1 ...
and use generate ordered list one:
- menu1 - submenu1 (a href="#") - subsubmenu1 (a href="$id") - subsubmenu2 (a href="$id") - subsubmenu3 (a href="#") - lastmenu1 (a href="$id") - menu2 (a href="#") - submenu1 (a href="$id")
how achieve using php (and jquery if needed)?
thank in advance.
$a = [ 1 => "menu1 > submenu1", 2 => "menu1 > submenu1 > subsubmenu1", 3 => "menu1 > submenu1 > subsubmenu2", 4 => "menu1 > submenu1 > subsubmenu3", 5 => "menu1 > submenu1 > subsubmenu3 > lastmenu1", 6 => "menu2 > submenu1 ", ]; function addmenuitem(&$arr, array $items, $id) { $a = &$arr; while ($name = array_shift($items)) { if (!isset($a[$name])) { $a[$name] = [ 'id' => $id, 'items' => [], ]; } $a = &$a[$name]['items']; } } $menu = []; foreach($a $id => $i) { $menuitem = explode(' > ', $i); addmenuitem($menu, $menuitem, $id); } var_dump($menu);
Comments
Post a Comment