我看过很多例子但是找不到确切的答案。回答的大多数问题都有错误,没有得到妥善回答。如果有人知道类似于我的问题的完成解决方案,请指出我的方向!
$menu = Array
(
[dashboard] => Array
(
[main] => /phone/summary
[main_selected] => 1
[child_selected] =>
[children] => Array
(
[phones] => /phones
[tablets] => /tablets
)
)
[mobile templates] => Array
(
[main] => /phone/templates
[main_selected] =>
)
[phone size] => Array
(
[main] => link_3
[main_selected] =>
[child_selected] =>
[children] => Array
(
[iphone] => /phone/iphone_size
[samsung] => /phone/samsung_size
[android] => /phone/android_size
[blackberry] => /phone/blackberry_size
[google_pixels] => /phone/google_phone_size
)
)
[phone chart] => Array
(
[main] => /phone/charts/
[main_selected] =>
)
)
以下是我想将其转化为:
<ul>
<li><a href="">Dashboard<a/>
<ul>
<li><a href="/phones">Iphone</a></li>
<li><a href="/tablets">Tablets</a></li>
</ul>
</li>
<li><a href="/phone/templates">mobile templates<a/>
</li>
<li><a href="link_3">phone size<a/>
<ul>
<li><a href="/phone/iphone_size">Iphone</a></li>
<li><a href="/phone/samsung_size">Samsung</a></li>
<li><a href="/phone/android_size">Android</a></li>
<li><a href="/phone/blackberry_size">Blackberry</a></li>
<li><a href="/phone/google_phone_size">Google_pixels</a></li>
</ul>
</li>
<li><a href="/phone/charts/">phone chart<a/></li>
</ul>
希望有人了解所显示的内容。 这就是我尝试过的。当然不是很好。
public function sideMenu($actions, $level = 0)
{
$xhtml = '';
$main_menu = null;
$ret = "";
$test = "";
foreach ($actions as $key => $value)
{
if(isset($value['children']))
{
$main_menu .= "<li>".$key."<li>";
//print_r($main_menu);
if (is_array($value["children"]))
{
$ret .= "<ul>";
$test = $value["children"];
foreach ($test as $k1 => $v1)
{
$ret .= "<li> ".$k1." </li>";
}
$ret .= "</ul>";
print_r($ret);
}
}
else
{
$main_menu .= "<li>".$key."</li>";
}
}
return $xhtml;
}
答案 0 :(得分:0)
public function sideMenu($actions){
$main_menu = "<ul>";
foreach ($actions as $key => $value)
{
$main_menu .= "<li><a href='".$value['main']."'>".$key."</a>";
if(isset($value['children']) && is_array($value['children'])){
$main_menu .= "<ul>";
foreach($value['children'] as $k => $v){
$main_menu .= "<li><a href='".$v."'>".$k."</a></li>";
}
$main_menu .= "</ul>";
}
$main_menu .= "</li>";
}
$main_menu .= "</ul>";
return $main_menu;
}
我已经更新了你的sideMenu功能,你可以通过它来调用它
echo $obj->sideMenu($menu);
其中$obj
是定义此函数的类对象。