本文介绍了如何创建一个UL - 李菜单,像阵一棵树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组标题孩子索引。

标题始终不能为空。 孩子是一个数组,空或不空。

任何孩子标题孩子等上。

  $ myArray的= [
    0 => [
        '标题'=> N1,
        '孩子'=>
            [
                0 =>
                    [
                        '标题'=> N11,
                        '孩子'=>
                            [
                                0 =>
                                    [
                                        '标题'=> N111,
                                        '孩子'=> []
                                    ]
                            ]
                    ]
            ]
    ]
    1 =>
        [
            '标题'=> N2,
            '孩子'=>
                [
                    0 =>
                        [
                            '标题'=> N21,
                            '孩子'=>
                                []
                        ]
                ]
        ]
];

现在,我想创建这个阵列的下拉菜单。

我有创造一个无序列表的问题( UL )的数组。

我想我的结果是这样的:

 < UL>
    <立GT; N1
        < UL>
            <立GT; N11
                < UL>
                    <立GT; N111< /李>
                < / UL>
            < /李>
        < / UL>
    < /李>
    <立GT; N2
        < UL>
            <立GT; N21< /李>
        < / UL>
    < /李>
< / UL>


解决方案

我敢肯定,这将工作:

 功能菜单($ ARR){
        回声< UL>中;
        的foreach($改编为$ VAL){            如果(!空($ VAL ['孩子'])){
                回声<立GT; 。 $ VAL ['标题'];
                菜单($ VAL ['孩子']);
                回声< /李>中;
            }其他{
                回声<立GT; 。 $ VAL ['标题']。 < /李>中;
            }
        }
        回声< / UL>中;
    }

I have an array with title and children index.

title is always not-null. children is an array, empty or not-empty.

Any children have title and children and so on.

$myArray = [
    0 => [
        'title'    => 'N1',
        'children' =>
            [
                0 =>
                    [
                        'title'    => 'N11',
                        'children' =>
                            [
                                0 =>
                                    [
                                        'title'    => 'N111',
                                        'children' => [],
                                    ],
                            ],
                    ],
            ],
    ],
    1 =>
        [
            'title'    => 'N2',
            'children' =>
                [
                    0 =>
                        [
                            'title'    => 'N21',
                            'children' =>
                                [],
                        ],
                ],
        ],
];

Now, I want to create a drop-down menu with this array.

I have problem with creating an unordered list (ul, li)from this array.

I want my result to be like:

<ul>
    <li>N1
        <ul>
            <li>N11
                <ul>
                    <li>N111</li>
                </ul>
            </li>
        </ul>
    </li>
    <li>N2
        <ul>
            <li>N21</li>
        </ul>
    </li>
</ul>
解决方案

I'm sure this will work :

    function menu($arr) {
        echo "<ul>";
        foreach ($arr as $val) {

            if (!empty($val['children'])) {
                echo "<li>" . $val['title'];
                menu($val['children']);
                echo "</li>";
            } else {
                echo "<li>" . $val['title'] . "</li>";
            }
        }
        echo "</ul>";
    }

这篇关于如何创建一个UL - 李菜单,像阵一棵树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:38