我需要一些递归下拉菜单的帮助。
这是我的阵列:
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
感谢您的帮助
答案 0 :(得分: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'] . ' - ');
}
}
}