我正在从 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>