doushantun0614
2013-08-06 12:33
浏览 60
已采纳

迭代菜单的多维数组

Hello I have a menu made in codeigniter. but I also want this to have submenu's

Therefore I get an array and go through it with a foreach loop.

        <ul>
            <?php foreach ($menu_item as $menu =>& $key): ?>    
            <li><?php echo anchor($menu, $key, $this->uri->slash_segment(1, 'leading') == $menu ? 'class="active"' : '') ?></li>
            <?php endforeach ?>
        </ul>

Now the problem is that this works great if its just one menu without submenu's but when I get an array like this

$menu_item = array(
    '/' => 'Home',
    '/about' => 'About',
    '/foo' => 'boo',
    '/contact' => 'contact',
    'test' => array(
               'foo' => 'boo'
              ),

    'test2' => 'foo2'
);

Than it doesn't work anymore. How can I loop through everything and output it as a good menu?

图片转代码服务由CSDN问答提供 功能建议

您好我有一个在codeigniter中制作的菜单。 但是我也希望这个有子菜单

因此我得到一个数组并用foreach循环完成它。

 &lt; ul&gt; 
&lt;?php foreach($ menu_item as $ menu =&gt;&amp; $ key):?&gt;  
&lt; li&gt;&lt;?php echo anchor($ menu,$ key,$ this-&gt; uri-&gt; slash_segment(1,'leading')== $ menu?'class =“active”':'  ')?&gt;&lt; / li&gt; 
&lt;?php endforeach?&gt; 
&lt; / ul&gt; 
   
 
 

现在的问题是这个 如果它只有一个没有子菜单的菜单,但是当我得到这样的数组

  $ menu_item = array(
'/'=&gt;'Home',
  '/ about'=&gt;'关于',
'/ foo'=&gt;'boo',
'/ contact'=&gt;'contact',
'test'=&gt;数组(
'  foo'=&gt;'boo'
),
 
'test2'=&gt;'foo2'
); 
   
 
 

比它没有' 再工作了。 如何循环浏览所有内容并将其作为一个好的菜单输出?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • donglu5000 2013-08-06 13:46
    已采纳

    The concept of the other answers is true, but they generate invalid DOM structure, so I decided to fix it.

    You can make a helper file and put the drawMenu() function inside. So, you'll be able to call the function as much as you need.

    $menu = array(
        '/'        => 'Home',
        '/about'   => 'About',
        '/foo'     => 'boo',
        '/contact' => 'contact',
        'test'     => array(
            'foo' => 'bar',
            'baz' => 'qux'
        ),
        'test2' => 'foo2'
    );
    
    function drawMenu($menu)
    {
        $CI =& get_instance();
    
        $output = '';
        foreach ($menu as $key => $value) {
            $output .= "<li>";
            if (is_array($value)) {
                $output .= anchor('#', $key);
                $output .= PHP_EOL."<ul>".PHP_EOL;
                $output .= drawMenu($value);
                $output .= "</ul>".PHP_EOL."</li>".PHP_EOL;
            } else {
                $output .= anchor($key, $value, $CI->uri->slash_segment(1, 'leading') == $key ? 'class="active"' : '');
                $output .= "</li>".PHP_EOL;
            }
        }
        return $output;
    }
    
    $html = drawMenu($menu);
    echo '<ul>'. $html .'</ul>';
    

    Side-note: Usage PHP_EOL constant is arbitrary, it just makes generated DOM more readable.


    Update:

    I improved the drawMenu() functionality, now you can add a URL address for the headers of sub-menus:

    $menu = array(
        '/'        => 'Home',
        '/about'   => 'About',
        '/foo'     => 'boo',
        '/contact' => 'contact',
        'test'     => array(
            'foo' => 'bar'
        ),
        'This is Test2|/url/to/test2' => array(
            'baz' => 'qux'
        )
    );
    

    You can add the URL after | separator.

    function drawMenu($menu)
    {
        $CI =& get_instance();
    
        $output = '';
        foreach ($menu as $key => $value) {
            $output .= "<li>";
    
            if (is_array($value)) {
    
                if (strpos($key, '|') !== false) {
                    $param = explode('|', $key);
                    $output .= anchor($param[1], $param[0]);
                } else {
                    $output .= anchor('#', $key);
                }
    
                $output .= PHP_EOL."<ul>".PHP_EOL;
                $output .= drawMenu($value);
                $output .= "</ul>".PHP_EOL."</li>".PHP_EOL;
            } else {
                $output .= anchor($key, $value, $CI->uri->slash_segment(1, 'leading') == $key ? 'class="active"' : '');
                $output .= "</li>".PHP_EOL;
            }
        }
        return $output;
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(4条)

相关推荐 更多相似问题