开发者

How to convert a multi-level array to ul?

开发者 https://www.devze.com 2022-12-13 22:11 出处:网络
I have the following array. Array ( [24] => Array ( [id] => 24 [name] => Main menu [shortdesc] => mainmenu

I have the following array.

    Array
(
    [24] => Array
        (
            [id] => 24
            [name] => Main menu
            [shortdesc] => mainmenu
            [status] => active
            [parentid] => 0
            [order] => 0
            [children] => Array
                (
                    [65] => Array
                        (
                            [id] => 65
                            [name] => Main 4
                            [shortdesc] => 
                            [status] => active
                            [parentid] => 24
                            [order] => 0
                            [children] => Array
                                (
                 开发者_JAVA百科               )

                        )

                    [64] => Array
                        (
                            [id] => 64
                            [name] => Main 3
                            [shortdesc] => 
                            [status] => active
                            [parentid] => 24
                            [order] => 0
                            [children] => Array
                                (
                                )

                        )

                   ...
                   ...

                    [59] => Array
                        (
                            [id] => 59
                            [name] => Main 1
                            [shortdesc] => 
                            [status] => active
                            [parentid] => 24
                            [order] => 0
                            [children] => Array
                                (
                                    [66] => Array
                                        (
                                            [id] => 66
                                            [name] => Main 1-1
                                            [shortdesc] => 
                                            [status] => active
                                            [parentid] => 59
                                            [order] => 0
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [25] => Array
        (
            [id] => 25
            [name] => Galleri 1
            [shortdesc] => galleri1
            [status] => active
            [parentid] => 0
            [order] => 0
            [children] => Array
                (
                )

        )

)

And I want to convert this to the following HTML by php.

<ul>
   <li>Main menu
        <ul>
            <li>Main 4</li>
            <li>Main 3</li>
            ...
            <li>Main 1
                <ul>
                    <li>Main 1-1</li>
                </ul>
            </li>
        </ul>
   </li>
   <li>Galleri 1</li>

</ul>

I started like this, but I am stuck.

if (count($navlist)){
  echo "\n<ul>";
  foreach ($navlist as $key => $list){
...
...


Something like this?:

function recursion($multi_dimensional_array)
    {
        $m = $multi_dimensional_array;

        $keys = array();
        foreach($m as $key=>$value)
        {
            $keys[] = $key;
        }

        $i = 0;
        while($i < count($multi_dimensional_array))
        {
            echo '<li><a href="#">'.$keys[$i].'</a>';
            if(is_array($multi_dimensional_array[$keys[$i]]))
            {
                echo '<ul>';
                recursion($multi_dimensional_array[$keys[$i]]);
                echo '</ul>';
            }
            echo '</li>';
            $i++;
        }
    }


function arrayToUL(array $array)
{
    echo '<ul>';
    foreach ($array as $key => $value)
    {
        if (isset($value['name'])) echo "<li>{$value['name']}</li>";
        if (!empty($value['children']) && is_array($value['children']))
        {
            echo arrayToUL($value['children']);
        }
    }
    echo '</ul>';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消