douyao3895 2014-03-10 17:50
浏览 177
已采纳

如何在php中打印出多维数组

Consider the array is:

Array
    (
    [Page-1] => Array
        (
            [0] => Array
                (
                    [0] => Cat-1
                    [1] => Item-1
                )
        )
    [Page-2] => Array
        (
            [0] => Array
                (
                    [0] => Cat-2
                    [1] => Item-2
                )
            [1] => Array
                (
                    [0] => Cat-3
                    [1] => Item-3
                )
            [2] => Array
                (
                    [0] => Cat-4
                    [1] => Item-4
                )
        )
    [Page-3] => Array
        (
            [0] => Array
                (
                    [0] => Cat-5
                    [1] => Item-5
                )
        )
    [Page-4] => Array
        (
            [0] => Array
                (
                    [0] => Cat-6
                    [1] => Item-6
                )
        )
    [Page-5] => Array
        (
            [0] => Array
                (
                    [0] => Cat-7
                    [1] => Item-7
                )
            [1] => Array
                (
                    [0] => Cat-9
                    [1] => Item-9
                )
        )
    [Page-6] => Array
        (
            [0] => Array
                (
                    [0] => Cat-8
                    [1] => Item-8
                )
        )
)

Where, the first keys [Page-x] from array will be Main-Links in the navigation menu.
Some of the main links may have Sub-Links, some not.
Sub-links are the values of the key [0] of the 3rd sub-array.
And finally the URL for each and every link will be the value of key [1] of the 3rd sub-Array.

Only Pages that have more than one category will show its categories as sub-links

The navigation bar i would like to have:

1. <a href="Item-1">Page-1</a>
2. <a href="#">Page-2</a>
     <a href="Item-2">Cat-2</a>
     <a href="Item-3">Cat-3</a>
     <a href="Item-4">Cat-4</a>
3. <a href="Item-5">Page-3</a>
4. <a href="Item-6">Page-4</a>
5. <a href="#">Page-5</a>
     <a href="Item-7">Cat-7</a>
     <a href="Item-9">Cat-9</a>
6. <a href="Item-8">Page-6</a>

the PHP code

$records = $p->main_links();

foreach ($records as $key => $value) {
    $return[$value['page']][] = array($value['child'], $value['item']);
}

foreach ($return as $key2 => $value2) {

    $count = 0;

    /* Select a specific value within the Array */
    $main_links = $value2[$count][1]; /* URL of the main Pages */

    $count = count($return[$key2]);

    if($count > 1) {
        foreach ($value2 as $key3 => $value3)
        {
            $link_name = $value3[0]; /* Child Link Names */
            $link_url  = $value3[1]; /* URL of Child Links */

            /* addedd htmlspecialchars() function to $variables that will be echoed into HTML. It provides some XSS protection */                
            $cat_link .= '<li><a href="'.htmlspecialchars($filter1.$p->seoUrl($key2).$filter2.$p->seoUrl($link_url)).'">'.htmlspecialchars($link_name).'</a></li>';
        }

        $result .= '
            <li '.htmlspecialchars($li_class).'><a href="#"><span>'.htmlspecialchars($key2).'</span></a>
                <ul>
                    '.$cat_link.'
                </ul>
            </li>';
    }else {
        $result .= '
            <li><a href="'.htmlspecialchars($filter1.$p->seoUrl($main_links)).'"><span>'.htmlspecialchars($key2).'</span></a></li>';
    }
}

Unfortunately i can't get it work... the output is not what i am expecting :(
current Output (wrong one):

1. <a href="Item-1">Page-1</a>
2. <a href="#">Page-2</a>
     <a href="Item-2">Cat-2</a>
     <a href="Item-3">Cat-3</a>
     <a href="Item-4">Cat-4</a>
3. <a href="Item-5">Page-3</a>
4. <a href="Item-6">Page-4</a>
5. <a href="#">Page-5</a>
     <a href="Item-2">Cat-2</a>
     <a href="Item-3">Cat-3</a>
     <a href="Item-4">Cat-4</a>
     <a href="Item-7">Cat-7</a>
     <a href="Item-9">Cat-9</a>
6. <a href="Item-8">Page-6</a>

Any help would be appreciated!

  • 写回答

2条回答 默认 最新

  • dongshen5115 2014-03-11 02:33
    关注

    I've found another way around which is way better than the one i was trying to do. This solved my case.

    http://wizardinternetsolutions.com/articles/web-programming/single-query-dynamic-multi-level-menu

    Thank you for your support!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图