PHP:使用foreach的三维数组的回声菜单?

时间:2015-04-26 17:07:03

标签: php arrays multidimensional-array foreach

我有这个多维数组来显示我的网站在某些PHP代码中使用的菜单。我一直在做一些实验和阅读,我想让它与多维数组一起工作,这样我就可以动态显示相应的菜单。

我有3个不同的菜单可以显示,"一个","两个"和"三个"。

$pages = array(
        'one' => array(
            'about 1' => array('services 1', 'partners 1'), 
            'events 1', 
            'contact 1'
        ), 
        'two' => array(
            'about 2' => array('services 2', 'partners 2'), 
            'events 2', 
            'contact 2'
        ), 
        'three' => array(
            'about 3' => array('services 3', 'partners 3'), 
            'events 3', 
            'contact 3'
        )
    );

到目前为止,我已尝试了一些方法,但我无法解决这个问题 - 我认为我的多维数组需要改变?

基本上如果$current_page == 'one'那么我想回显'one'的相应数组。

它必须是这样的:

<?php
    echo '<ul class="menu">';
    if ($current_page == 'one'){
        foreach($pages['one'] as $page => $dropdown){
            if (array has third dimension){ // pseudo code if pages have third dimension
                echo '<li class="has-dropdown">' $page '</li>\n';
                echo '<ul class="dropdown">';
                foreach($pages['one'] as $page => $dropdown){
                    echo '<li>' . $dropdown . '</li>\n';
                }
                echo '</ul>';
            } else { // no third dimension
                foreach($pages['one'] as $page){
                    echo '<li>' $page '</li>\n';
                }
            }
        }
    }
    elseif ($current_page == 'two'){
        foreach($pages['two'] as $page){
            if (array has third dimension){ // pseudo code if pages have third dimension
                echo '<li class="has-dropdown">' $page '</li>\n';
                echo '<ul class="dropdown">';
                foreach($pages['one'] as $page => $dropdown){
                    echo '<li>' . $dropdown . '</li>\n';
                }
                echo '</ul>';
            } else { // no third dimension
                foreach($pages['one'] as $page){
                    echo '<li>' $page '</li>\n';
                }
            }
        }
    }
    elseif ($current_page == 'three'){
        foreach($pages['three'] as $page){
            if (array has third dimension){ // pseudo code if pages have third dimension
                echo '<li class="has-dropdown">' $page '</li>\n';
                echo '<ul class="dropdown">';
                foreach($pages['one'] as $page => $dropdown){
                    echo '<li>' . $dropdown . '</li>\n';
                }
                echo '</ul>';
            } else { // no third dimension
                foreach($pages['one'] as $page){
                    echo '<li>' $page '</li>\n';
                }
            }
        }
    }
    echo '</ul>';
?>

以上不起作用,它只是我一直在尝试的粗略画面。问题是:我如何使用foreach循环显示我的菜单?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

我真的不知道该说什么,这只是代码。研究它并询问您是否有任何问题,我将很乐意为您提供帮助!

请注意,此代码仅处理3维数组,如果需要添加更多子级别,最好查看递归,但可能是一个更复杂的主题。

$pages = array(
    'one' => array(
        'about 1' => array('services 1', 'partners 1'),
        'events 1',
        'contact 1'
    ),
    'two' => array(
        'about 2' => array('services 2', 'partners 2'),
        'events 2',
        'contact 2'
    ),
    'three' => array(
        'about 3' => array('services 3', 'partners 3'),
        'events 3',
        'contact 3'
    )
);

$current_page = 'one';

echo '<ul class="menu">';
if (array_key_exists($current_page, $pages)) {
    foreach($pages[$current_page] as $item => $items) {
        echo '<li class="has-dropdown">';


        if (is_array($items)) {
            echo $item . '<ul class="dropdown">';
            foreach($items as $page => $dropdown) {
                echo '<li>'.$dropdown.
                '</li>\n';
            }
            echo '</ul>';
        }else{
            echo $items;
        }

        echo '</li>';
    }
}

echo '</ul>';