PHP function that creates a nested ul li?

前端 未结 5 691
隐瞒了意图╮
隐瞒了意图╮ 2021-01-07 15:18

I am attemptting to attach a small CMS to a website I am creating. However I have come across a small problem. The CMS uses PHP functions for inserting menus, these PHP func

5条回答
  •  走了就别回头了
    2021-01-07 15:58

    I think this outputs the correct HTML structure.

    function GenerateMenu($arr)
    {
    foreach($arr as $val) {
        if (next($arr)) { $nextitem = current($arr); }
        if (is_array($val)) {
            $output .= "\n
      \n" . GenerateMenu($val, $output) . "
    \n
  • \n\n"; } elseif (is_array($nextitem)) { $output .= "
  • " . $val; } else { $output .= "
  • " . $val . "
  • \n"; } } return $output; } $html = GenerateMenu($menu); echo("
      \n" . $html . '
    ');

    Same test code as above:

    $menu = array('Projects (Menu Level 1)',
              array('Project 1 (Menu Level 2)',
                    'Project 2 (Menu Level 2)',
                    'Project 3 (Menu Level 2)'),
              'News (Menu Level 1)',
              'Contact (Menu Level 1)');
    

提交回复
热议问题