我有一个数组,其中我保留了一个包含100多个元素的菜单,然后将其打印为CSS下拉菜单。有5个主菜单,其中有子菜单,其中一些有子菜单等等。现在我在打印它时很难对循环进行编码,但我确信必须有一些聪明的方法来做只需几行代码!这就是数组的开头:
$menu = array(
array(
'title' => 'Travel tips',
'url' => 'travel-tips',
'sub' => array(
array(
'title' => 'Travel guide',
'url' => 'travel-guide'),
array(
'title' => 'Places to visit',
'url' => 'places-to-visit',
'sub' => array(
array(
'title' => 'Ahu Akivi',
'url' => 'ahu-akivi'),
array(
'title' => 'Ahu Tongariki',
'url' => 'ahu-tongariki'),
array(
'title' => 'Anakena',
'url' => 'anakena'),
array(
'title' => 'Orongo',
'url' => 'orongo'),
array(
'title' => 'Rano Kau',
'url' => 'rano-kau'),
array(
'title' => 'Rano Raraku',
'url' => 'rano-raraku'),
array(
'title' => 'Vinapu',
'url' => 'vinapu'))),
array(
'title' => 'Things to do',
'url' => 'things-to-do',
'sub' => array(
array(
'title' => 'Beaches',
'url' => 'beaches'),
array(
'title' => 'Church',
'url' => 'church'),
array(
'title' => 'Fishing',
'url' => 'fishing'),
......然后它继续下去。如何在不对代码进行硬编码的情况下,以递归的方式在几行代码中整齐而干净地循环这个?
答案 0 :(得分:2)
如果您不想指定循环次数,则需要自我调用功能。只需在每个循环中添加所需的div / class /格式。
function echoMenu($arr){
foreach($arr as $subArr){
if(!empty($subArr['sub'])){
echo "<a href='{$subArr['url']}'>{$subArr['title']}</a>";
echo "<div class='for-sub-links'>";
echoMenu($subArr['sub']);
echo "</div>";
}else{
echo "<div><a href='{$subArr['url']}'>{$subArr['title']}</a></div>";
}
}
}
答案 1 :(得分:1)
function recursive_menu($menu) {
$output ='';
foreach($menu as $m){
$output .= '<li><a href="'.$m['url'].'">'.$m['title'].'</a>';
if (isset($m['sub'])) {
$output .= '<ul>';
foreach( $m['sub'] as $item ) {
$output.=recursive_menu($item);
}
$output .= '</ul>';
}
}
return $output.'</li>';
}
...然后
echo '<ul>'.recursive_menu($menu).'</ul>';
像这样......
答案 2 :(得分:1)
相当简单:
//mainly for demonstration / test purposes
function drawMenuItem($title, $url, $indent) {
for ($i=0;$i<=$indent;$i++) echo "___";
echo '<a href="'.$url.'">'.$title.'</a>'.'<br>';
}
//the actual "few lines of code recursively" function
function createMenuRecursively($menu, $indent) {
foreach($menu as $menuItem){
drawMenuItem($menuItem['title'], $menuItem['url'], $indent);
if (is_array($menuItem['sub'])) {
createMenuRecursively($menuItem['sub'], $indent+1);
}
}
}
createMenuRecursively($menu, 1);
只需使用$indent
进行演示。覆盖drawMenuItem
以执行CSS
/ ul
/ li
内容。以上输出:
覆盖
答案 3 :(得分:0)
感谢aynber,我提出了一个解决方案,它也提供了正确的URL:
function echoMenu($arr, $prevURL)
{
if (is_array($prevURL))
{
foreach($prevURL as $url) //Collect all previous URLs into string
$prevURLStr .= $url . '/';
}
foreach($arr as $key => $subArr)
{
echo '<li><a href="/' . $prevURLStr . $subArr['url'] . '">' . $subArr['title'] . '</a>';
if(!empty($subArr['sub'])) //Has submenu - call function again to enter it
{
$prevURL[] = $subArr['url']; //Add this folder's URL to array
echo '<ul>';
echoMenu($subArr['sub'], $prevURL);
echo '</ul>';
$prevURL = array_pop($prevURL); //Remove last element
$prevURL = @array_values($prevURL); //Remove empty remains
}
echo '</li>';
}
}
echo '<ul class="menu">';
echoMenu($menu, null);
echo '</ul>';