我目前正在回显来自查询的数组,我需要将它们呈现在嵌套的无序列表中。我正在尝试找到一种方法来无限地执行此过程,因为我目前正在手动回显每个嵌套的 ul(如果它们有的话),直到我手动编码的深度。如果有办法自动回显 ul 的深度“n”,请帮助我。
这是我目前的逻辑,只回显到深度 5:
<ul class="parent_tree"><?php
foreach($category_tree as $category_level_1)
{
echo '<li><a href="#">' . $category_level_1['name'] . '</a>';
if(array_key_exists('children', $category_level_1))
{
echo '<ul>';
foreach($category_level_1['children'] as $category_level_2)
{
echo '<li><a href="#">' . $category_level_2['name'] . '</a>';
if(array_key_exists('children', $category_level_2))
{
echo '<ul>';
foreach($category_level_2['children'] as $category_level_3)
{
echo '<li><a href="#">' . $category_level_3['name'] . '</a>';
if(array_key_exists('children', $category_level_3))
{
echo '<ul>';
foreach($category_level_3['children'] as $category_level_4)
{
echo '<li><a href="#">' . $category_level_4['name'] . '</a>';
if(array_key_exists('children', $category_level_4))
{
echo '<ul>';
foreach($category_level_4['children'] as $category_level_5)
{
echo '<li><a href="#">' . $category_level_5['name'] . '</a>';
//level 6 goes here
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
}
?>
</ul>