标签:style blog io ar color sp for 数据 on
从数据库获取所有菜单信息,需要根据id,pid字段获取主菜单及其子菜单,以及子菜单下的子菜单,可以通过函数递归来实现。
<?php class Menu { public $menu = array(); public function __construct() { $this->menu = array( array(‘id‘=>1, ‘title‘=>"首页",‘pid‘=>0), array(‘id‘=>158,‘title‘=>"基础信息",‘pid‘=>157), array(‘id‘=>159,‘title‘=>"产消日志",‘pid‘=>157), array(‘id‘=>147,‘title‘=>"玩家管理",‘pid‘=>124), array(‘id‘=>148,‘title‘=>"踢线封停禁言",‘pid‘=>147), array(‘id‘=>149,‘title‘=>"游戏公告",‘pid‘=>147), array(‘id‘=>150,‘title‘=>"礼包卡",‘pid‘=>147), array(‘id‘=>151,‘title‘=>"邮件发送",‘pid‘=>147), array(‘id‘=>152,‘title‘=>"全服邮件发送",‘pid‘=>147), array(‘id‘=>160,‘title‘=>"玩家调整",‘pid‘=>147), array(‘id‘=>154,‘title‘=>"审批管理",‘pid‘=>147), array(‘id‘=>155,‘title‘=>"GM指令",‘pid‘=>147), array(‘id‘=>156,‘title‘=>"服务器参数配置",‘pid‘=>147), array(‘id‘=>157,‘title‘=>"日志查询",‘pid‘=>124), array(‘id‘=>123,‘title‘=>"数据查询",‘pid‘=>0), array(‘id‘=>124,‘title‘=>"游戏管理",‘pid‘=>0), array(‘id‘=>126,‘title‘=>"系统工具",‘pid‘=> 0), array(‘id‘=>127,‘title‘=>"权限管理",‘pid‘=> 0), array(‘id‘=>128,‘title‘=>"数据总览",‘pid‘=>123), array(‘id‘=>129,‘title‘=>"登录建角与在线数据",‘pid‘=>123), array(‘id‘=>130,‘title‘=>"留存数据分析",‘pid‘=>123), array(‘id‘=>131,‘title‘=>"付费统计",‘pid‘=>123), array(‘id‘=>132,‘title‘=>"消耗数据分析",‘pid‘=>123), array(‘id‘=>134,‘title‘=>"实时统计",‘pid‘=>129), array(‘id‘=>135,‘title‘=>"历史统计",‘pid‘=>129), array(‘id‘=>137,‘title‘=>"日留存",‘pid‘=>130), array(‘id‘=>138,‘title‘=>"等级&任务留存",‘pid‘=>130), array(‘id‘=>139,‘title‘=>"分职业统计",‘pid‘=>130), array(‘id‘=>140,‘title‘=>"日充值统计",‘pid‘=>131), array(‘id‘=>141,‘title‘=>"充值订单",‘pid‘=> 131), array(‘id‘=>142,‘title‘=>"充值排行",‘pid‘=>131), array(‘id‘=>143,‘title‘=>"货币消耗",‘pid‘=>132), array(‘id‘=>144,‘title‘=>"货币日留存",‘pid‘=>132), array(‘id‘=>145,‘title‘=>"用户管理",‘pid‘=>127), array(‘id‘=>146,‘title‘=>"用户组管理",‘pid‘=>127) ); return $this->getMenu(); } /** * 主菜单pid为0 * @return array */ protected function getMenu() { foreach ($this->menus as $key => $items) { if ($items[‘pid‘] == "0" ) { unset($this->menu[$key]); $menu[] = $this->buildMenuTree($items, $items[‘id‘]); } } return $menu; } /** * 生产多级菜单树 * @param array $items * @param int $rid * @return array */ protected function buildMenuTree($items,$rid) { $childs = $this->getChildMenu($items, $rid); if (isset($childs[‘child‘])) { foreach ($childs[‘child‘] as $key => $value) { $children = $this->buildMenuTree($value, $value[‘id‘]); if (null != $children[‘child‘]) { $childs[‘child‘][$key][‘child‘] = $children[‘child‘]; } } } return $childs; } /** * 获取子菜单 * */ protected function getChildMenu($items,$rid) { foreach ($this->menu as $key => $value) { if ($value[‘pid‘] == $rid) { unset($this->menu[$key]); $items[‘child‘][] = $value; } } return $items; } } var_dump(new Menu);
测试结果为:
array(5) {
[0] => array(4) {
["id"] => string(1) "1"
["title"] => string(6) "首页"
["pid"] => string(1) "0"
["sort"] => string(1) "1"
}
[1] => array(5) {
["id"] => string(3) "123"
["title"] => string(12) "数据查询"
["pid"] => string(1) "0"
["sort"] => string(1) "1"
["child"] => array(5) {
[0] => array(4) {
["id"] => string(3) "128"
["title"] => string(12) "数据总览"
["pid"] => string(3) "123"
["sort"] => string(1) "0"
}
[1] => array(5) {
["id"] => string(3) "129"
["title"] => string(27) "登录建角与在线数据"
["pid"] => string(3) "123"
["sort"] => string(1) "0"
["child"] => array(2) {
[0] => array(4) {
["id"] => string(3) "134"
["title"] => string(12) "实时统计"
["pid"] => string(3) "129"
["sort"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(3) "135"
["title"] => string(12) "历史统计"
["pid"] => string(3) "129"
["sort"] => string(1) "0"
}
}
}
[2] => array(5) {
["id"] => string(3) "130"
["title"] => string(18) "留存数据分析"
["pid"] => string(3) "123"
["sort"] => string(1) "0"
["child"] => array(3) {
[0] => array(4) {
["id"] => string(3) "137"
["title"] => string(9) "日留存"
["pid"] => string(3) "130"
["sort"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(3) "138"
["title"] => string(19) "等级&任务留存"
["pid"] => string(3) "130"
["sort"] => string(1) "0"
}
[2] => array(4) {
["id"] => string(3) "139"
["title"] => string(15) "分职业统计"
["pid"] => string(3) "130"
["sort"] => string(1) "0"
}
}
}
[3] => array(5) {
["id"] => string(3) "131"
["title"] => string(12) "付费统计"
["pid"] => string(3) "123"
["sort"] => string(1) "0"
["child"] => array(3) {
[0] => array(4) {
["id"] => string(3) "140"
["title"] => string(15) "日充值统计"
["pid"] => string(3) "131"
["sort"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(3) "141"
["title"] => string(12) "充值订单"
["pid"] => string(3) "131"
["sort"] => string(1) "0"
}
[2] => array(4) {
["id"] => string(3) "142"
["title"] => string(12) "充值排行"
["pid"] => string(3) "131"
["sort"] => string(1) "0"
}
}
}
[4] => array(5) {
["id"] => string(3) "132"
["title"] => string(18) "消耗数据分析"
["pid"] => string(3) "123"
["sort"] => string(1) "0"
["child"] => array(2) {
[0] => array(4) {
["id"] => string(3) "143"
["title"] => string(12) "货币消耗"
["pid"] => string(3) "132"
["sort"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(3) "144"
["title"] => string(15) "货币日留存"
["pid"] => string(3) "132"
["sort"] => string(1) "0"
}
}
}
}
}
[2] => array(5) {
["id"] => string(3) "124"
["title"] => string(12) "游戏管理"
["pid"] => string(1) "0"
["sort"] => string(1) "3"
["child"] => array(2) {
[0] => array(5) {
["id"] => string(3) "147"
["title"] => string(12) "玩家管理"
["pid"] => string(3) "124"
["sort"] => string(1) "0"
["child"] => array(9) {
[0] => array(4) {
["id"] => string(3) "148"
["title"] => string(18) "踢线封停禁言"
["pid"] => string(3) "147"
["sort"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(3) "149"
["title"] => string(12) "游戏公告"
["pid"] => string(3) "147"
["sort"] => string(1) "0"
}
[2] => array(4) {
["id"] => string(3) "150"
["title"] => string(9) "礼包卡"
["pid"] => string(3) "147"
["sort"] => string(1) "0"
}
[3] => array(4) {
["id"] => string(3) "151"
["title"] => string(12) "邮件发送"
["pid"] => string(3) "147"
["sort"] => string(1) "0"
}
[4] => array(4) {
["id"] => string(3) "152"
["title"] => string(18) "全服邮件发送"
["pid"] => string(3) "147"
["sort"] => string(1) "0"
}
[5] => array(4) {
["id"] => string(3) "160"
["title"] => string(12) "玩家调整"
["pid"] => string(3) "147"
["sort"] => string(1) "0"
}
[6] => array(4) {
["id"] => string(3) "154"
["title"] => string(12) "审批管理"
["pid"] => string(3) "147"
["sort"] => string(1) "0"
}
[7] => array(4) {
["id"] => string(3) "155"
["title"] => string(8) "GM指令"
["pid"] => string(3) "147"
["sort"] => string(1) "0"
}
[8] => array(4) {
["id"] => string(3) "156"
["title"] => string(21) "服务器参数配置"
["pid"] => string(3) "147"
["sort"] => string(1) "0"
}
}
}
[1] => array(5) {
["id"] => string(3) "157"
["title"] => string(12) "日志查询"
["pid"] => string(3) "124"
["sort"] => string(1) "0"
["child"] => array(2) {
[0] => array(4) {
["id"] => string(3) "158"
["title"] => string(12) "基础信息"
["pid"] => string(3) "157"
["sort"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(3) "159"
["title"] => string(12) "产消日志"
["pid"] => string(3) "157"
["sort"] => string(1) "0"
}
}
}
}
}
[3] => array(4) {
["id"] => string(3) "126"
["title"] => string(12) "系统工具"
["pid"] => string(1) "0"
["sort"] => string(1) "4"
}
[4] => array(5) {
["id"] => string(3) "127"
["title"] => string(12) "权限管理"
["pid"] => string(1) "0"
["sort"] => string(1) "5"
["child"] => array(2) {
[0] => array(4) {
["id"] => string(3) "145"
["title"] => string(12) "用户管理"
["pid"] => string(3) "127"
["sort"] => string(1) "0"
}
[1] => array(4) {
["id"] => string(3) "146"
["title"] => string(15) "用户组管理"
["pid"] => string(3) "127"
["sort"] => string(1) "0"
}
}
}
}
标签:style blog io ar color sp for 数据 on
原文地址:http://www.cnblogs.com/zz123/p/4115675.html