1

我需要一些关于递归下拉菜单的帮助。

这是我的数组:

Array
(
    [0] => Array
        (
            [cat_id] => 4
            [cat_name] => Plátna
            [cat_nodia] => Platna
            [cat_child] => 0
            [cat_desc] => 
            [cat_main] => 1
            [cat_order] => 4
            [parent] => 
            [sub] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 16
                            [cat_name] => Ľanové
                            [cat_nodia] => Lanove
                            [cat_child] => 4
                            [cat_desc] => 
                            [cat_main] => 0
                            [cat_order] => 16
                            [parent] => Ľanové
                            [sub] => 
                        )

                    [1] => Array
                        (
                            [cat_id] => 17
                            [cat_name] => Bavlnené
                            [cat_nodia] => Bavlnene
                            [cat_child] => 4
                            [cat_desc] => 
                            [cat_main] => 0
                            [cat_order] => 17
                            [parent] => Bavlnené
                            [sub] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 21
                                            [cat_name] => 10x10
                                            [cat_nodia] => 10x10
                                            [cat_child] => 17
                                            [cat_desc] => 
                                            [cat_main] => 0
                                            [cat_order] => 21
                                            [parent] => 10x10
                                            [sub] => 
                                        )

                                    [1] => Array
                                        (
                                            [cat_id] => 22
                                            [cat_name] => 20x25
                                            [cat_nodia] => 20x25
                                            [cat_child] => 17
                                            [cat_desc] => 
                                            [cat_main] => 0
                                            [cat_order] => 22
                                            [parent] => 20x25
                                            [sub] => 
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [cat_id] => 5
            [cat_name] => Uhlík
            [cat_nodia] => Uhlik
            [cat_child] => 0
            [cat_desc] => 
            [cat_main] => 0
            [cat_order] => 5
            [parent] => 
            [sub] => 
        )
)

我想制作如下所示的下拉菜单:

Platna
Platna - Lanove
Platna - Bavlnene
Platna - Bavlnene - 10x10
Platna - Bavlnene - 20x25
Uhlik

这是我迄今为止尝试过的:

   function show_menu($array)
    {
    //  print_r($array);
        foreach($array as $val)
        {
            if (is_array($val['sub']))
            {
                echo $val['cat_name'];
                $this->show_menu($val['sub']);
            }   else
            {
                echo $val['cat_name'];
            }
            echo "<br>";
        }
    }

这给了我这样的感觉:

Plátna Ľanové
Bavlnené 10x10
20x25 

谢谢你的帮助

4

1 回答 1

1

尝试这个

   function show_menu($array, $name='')
    {
    //  print_r($array);
        foreach($array as $val)
        {
            echo $name . $val['cat_name'] . "<br>";
            if (is_array($val['sub']))
            {
                $this->show_menu($val['sub'], $name . $val['cat_name'] . ' - ');
            }
        }
    }
于 2013-08-01T16:48:35.673 回答