从数组创建HTML列表

时间:2016-03-30 04:58:24

标签: php html

我希望从数组中创建一个列表,但不仅仅是任何数组,不,这太简单了。该数组如下所示:

Array
(
    [0] => Array
        (
            [id] => 1
            [content] => Lost my heart and a leg in Australia
            [path] => {1}
            [depth] => 1
        )

    [1] => Array
        (
            [id] => 5
            [content] => I left my heart in sandcrabs bistro
            [path] => {1,5}
            [depth] => 2
        )

    [2] => Array
        (
            [id] => 7
            [content] => I left my heart in San Francisco
            [path] => {1,5,7}
            [depth] => 3
        )

    [3] => Array
        (
            [id] => 9
            [content] => That is the worst joke ever.
            [path] => {1,5,7,9}
            [depth] => 4
        )
    [4] => Array
        (
            [id] => 10
            [content] => Worst joke evar.
            [path] => {1,5,7,10}
            [depth] => 4
        )

    [5] => Array
        (
            [id] => 8
            [content] => What happened to mudcrab?
            [path] => {1,5,8}
            [depth] => 3
        )

    [6] => Array
        (
            [id] => 6
            [content] => I am the tinman, and have no heart
            [path] => {1,6}
            [depth] => 2
        )

    [7] => Array
        (
            [id] => 4
            [content] => Even on land there are loan sharks
            [path] => {4}
            [depth] => 1
        )

)

并且HTML应该如下所示:

<ul><!-- level 1 -->
    <li>Lost my heart and a leg in Australia</li>
    <li>
        <ul><!-- level 2 -->
            <li>I left my heart in sandcrabs bistro</li>
            <li>
                <ul><!-- level 3 -->
                    <li>I left my heart in San Francisco</li>
                    <li>
                        <ul><!-- level 4 -->
                            <li>That is the worst joke ever</li>
                            <li>Worst joke evar</li>
                        </ul><!-- end level 4 -->
                    <li>What happened to mudcrab?</li>
                </ul><!-- end level 3 -->
            <li>I am the tinman, and have no heart</li>
        </ul><!-- end level 2 -->
    <li>Even on land there are loan sharks</li>
</ul><!-- end level 1 -->

有关如何正确理解水平/深度的任何建议都非常感激。

0 个答案:

没有答案