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 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合