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

迭代菜单的多维数组

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?

  • 写回答

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里