我正在从php数组生成菜单
$MENU["HOME"] = array( 'enabled'=>true, 'text'=>'Home' ,'link'=> 'public/home' );
$MENU["SHOP"] = array( 'enabled'=>true, 'text'=>'Shop' ,'link'=> 'public/shop' );
生成菜单的代码是
function show_menu(){
$menu_string = '<ul>';
foreach( $MENU as $item )
{
if( $item['enabled'] )
{
$menu_string .= '<li>'.$item['text'].'</li>';
}
}
echo $menu_string.'</ul>';
}
但现在我有很多级别的菜单如下,所以我将配置更改为
$MENU["HOME"] = array
(
'parent'=>true, // parent == true ? show in top level menu : do not show in top level
'enabled'=>true,
'text'=>'Home',
'link'=> 'public/home',
'sub_modules' => array() // empty sub modules means no sub menus need to display
);
$MENU["SHOP"] = array
(
'parent'=>true,
'enabled'=>true,
'text'=>'Shop',
'link'=> 'public/shop',
'sub_modules' => array('SALES') // SALES is a sub menu of SHOP,which is also configured as another module
);
$MENU["SALES"] = array
(
'parent'=>FALSE, // PARENT = FALSE (this is a sub menu of SHOP)
'enabled'=>true,
'text'=>'Sales',
'link'=> 'public/shop/sales',
'sub_modules' => array('SALES_RETURN','SALES_REPORT') //have 2 sub menu's
);
$MENU["SALES_RETURN"] = array
(
'parent'=>FALSE,
'enabled'=>true,
'text'=>'Sales Return',
'link'=> 'public/shop/sales/return',
'sub_modules' => array()
);
$MENU["SALES_REPORT] = array
(
'parent'=>FALSE,
'enabled'=>true,
'text'=>'Sales Report',
'link'=> 'public/shop/sales/report',
'sub_modules' => array()
);
现在我无法使用相同的功能,因为菜单越来越深,
如果parent
为true
,则表示该特定模块/菜单已启用,
我怎样才能做到这一点? (菜单可能有子模块,但子模块也可能有其他菜单,这就是问题)
预期结果是
<UL>
<li>Home</li>
<li>
Shop
<UL>
<LI>
SALES
<UL>
<LI>Sales Return</LI>
<LI>Sales Report</LI>
</UL>
</LI>
</UL>
</li>
</UL>
答案 0 :(得分:0)
使用递归函数很容易,我会给你一个第一个版本(未经测试):
function parse_menu($menu){
echo "<li>".$menu['text']."</li>"
if(!empty($menu['sub_modules'])){
echo "<ul>";
foreach($menu['sub_modules'] as $e){
parse_menu($menus_array[$e]); //or whatever you are calling the generic array
}
echo "</ul>";
}
}
你应该调用每个没有父级的$ menus_array元素(树的0级)。
答案 1 :(得分:0)
测试,工作......
<?php
$MENU=array();
$MENU["HOME"] = array
(
'parent'=>true, // parent == true ? show in top level menu : do not show in top level
'enabled'=>true,
'text'=>'Home',
'link'=> 'public/home',
'sub_modules' => array() // empty sub modules means no sub menus need to display
);
$MENU["SHOP"] = array
(
'parent'=>true,
'enabled'=>true,
'text'=>'Shop',
'link'=> 'public/shop',
'sub_modules' => array('SALES') // SALES is a sub menu of SHOP,which is also configured as another module
);
$MENU["SALES"] = array
(
'parent'=>FALSE, // PARENT = FALSE (this is a sub menu of SHOP)
'enabled'=>true,
'text'=>'Sales',
'link'=> 'public/shop/sales',
'sub_modules' => array('SALES_RETURN','SALES_REPORT') //have 2 sub menu's
);
$MENU["SALES_RETURN"] = array
(
'parent'=>FALSE,
'enabled'=>true,
'text'=>'Sales Return',
'link'=> 'public/shop/sales/return',
'sub_modules' => array()
);
$MENU["SALES_REPORT"] = array
(
'parent'=>FALSE,
'enabled'=>true,
'text'=>'Sales Report',
'link'=> 'public/shop/sales/report',
'sub_modules' => array()
);
function show_menu(&$MENU,$subIndex=false){
$menu_string = '<UL>';
if(!$subIndex){
foreach($MENU as $item)
{
if( $item['enabled']&&$item['parent'] )
{
$_subString="";
if(!empty($item['sub_modules'])){
foreach($item['sub_modules'] as $sub){
$_subString .= show_menu($MENU,$sub);
}
}
$menu_string .= '<LI>'.$item['text'].$_subString.'</LI>';
}
}
}else{
if(@$MENU[$subIndex]['enabled']&&!@$MENU[$subIndex]['parent'])
{
$_subString="";
if(!empty($MENU[$subIndex]['sub_modules'])){
foreach($MENU[$subIndex]['sub_modules'] as $sub){
$_subString .= show_menu($MENU,$sub);
}
}
$menu_string .= '<LI>'.$MENU[$subIndex]['text'].$_subString.'</LI>';
}
}
return $menu_string.'</UL>';
}
echo show_menu($MENU);
?>
答案 2 :(得分:-1)
仅仅因为其他两个答案根本不是这个问题的解决方案,我在这个主题中搜索了几天,发现了相当不错且有效的解决方案there。我发布这个是因为其他人在将来阅读这篇文章,并希望这对我和任何人都有帮助。此外,还必须更改阵列的样式。阅读整篇文章。
编辑: 这可能是数组
$menu = array(
'about' => array(
'display' => 'About Us'),
'blog' => array(
'display' => 'Read Our Blog'),
'links' => array(
'display' => 'Recommended Links',
'sub' => array(
'products' => array(
'display' => 'High-Quality Products',
'url' => 'links/#products'),
'services' => array(
'display' => 'Helpful Services',
'url' => 'links/#services',
'sub' => array(
'local' => array(
'display' => 'Local Services',
'url' => 'links/#services_local'),
'online' => array(
'display' => 'Online Services',
'url' => 'links/#services_online')
)))),
'contact' => array(
'display' => 'Contact Us'
));
这就是功能。我测试了它,它正在工作。
function buildMenu($menu_array, $is_sub = FALSE){
$attr =
(!$is_sub) ? ' id="menu"' : ' class="submenu"';
$menu = "<ul$attr>";
foreach($menu_array as $id => $properties) {
foreach($properties as $key => $val) {
if (is_array($val)) {
$sub = buildMenu($val, TRUE);
}else{
$sub = NULL;
$$key = $val;
}}
if (!isset($url)) {
$url = $id;
}
$menu.="<li><a href=" . $url . ">$display</a>$sub</li>";
unset($url, $display, $sub);
}
return $menu . "</ul>";
}
只需在代码中的某处调用echo buildMenu($menu);
即可。输出预期的代码:
<ul id="menu">
<li><a href=about>About Us</a></li>
<li><a href=blog>Read Our Blog</a></li>
<li>
<a href=links>Recommended Links</a>
<ul class="submenu">
<li><a href=links/#products>High-Quality Products</a></li>
<li>
<a href=links/#services>Helpful Services</a>
<ul class="submenu">
<li><a href=links/#services_local>Local Services</a></li>
<li><a href=links/#services_online>Online Services</a></li>
</ul>
</li>
</ul>
</li>
<li><a href=contact>Contact Us</a></li>
</ul>