php递归获取无限分类菜单

从数据库获取所有菜单信息,需要根据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"
      }
    }
  }
}
时间: 2024-10-10 01:06:44

php递归获取无限分类菜单的相关文章

php不用递归完成无限分类,从表设计入手完整演示过程

无限分类是什么就不废话了,可以用递归实现,但是递归从数据库取东西用递归效率偏低,如果从表设计入手,就很容易做到网站导航的实现,下面是某论坛导航,如下图 网上无限分类大多不全面,今天我会从设计表开始, 首先我们先做视图界面, <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>白超华-博客园</title> &

TreeView递归绑定无限分类数据

实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的: 字段 类型 Id int ParentId int Name Nvarchar(64) 实现数据绑定: private void ControlsDataBind() { tvCategory.Nodes.Clear(); List<Models.Category> types = CommonNews.Helper.OperateContext.Current.LoadNewsTypes();

不用递归实现无限分类数据的树形格式化

<?php /** * 此方法由@Tonton 提供 * http://my.oschina.net/u/918697 * @date 2012-12-12 */ function genTree5($items) { foreach ($items as $item) $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]; return isset($items[0]['son']) ? $items[0]['son

无限分类树操作

获取相应分类id的分类树: public static function getCategoryTree($id){ //$model=M('category'); if($id>0){ $obj=self::selectTable('category',array('id'=>$id),true);//$model->where(array('id'=>$id))->find(); if(!is_null($obj)){ $childList=self::selectTab

深度实现session【包括session入库、session机制和session和cookie的使用方法,完善会话机制(在分布式机器中也能使用)】、无限分类的实现

1.session的注意点:@session_start();//这个配置需要注意,session开启中会有影响,所以使用错误抑制符进行限制[并且使用php.ini对session进行自动开启] session_start()前的输出问题:[session信息本身会增加到http头信息,也就是http主体不能在头前]对php.ini中的输出缓存进行配置,out_buffer的配置[注意:开启之后能够保证输出内容在脚本中缓存] [注意](1)脚本中session变量的键只能是字符串类型的[$_SE

初学递归,接触无限分类,小小的尝试

今日学习的是无限循环,这与递归有关.其实我自己还不是完全熟悉和上手,还需要琢磨琢磨. 初步学习递归,算是无限分类的一个小练习. 表:cate id int auto 自动递增 pid int 0为最大的分类 catename varchar 分类名称 读取数据库的cate表分类如下: //分类练习 include '../include.php'; function getList($pid=0,&$result=array(),$spac=0) { $spac = $spac + 4; $sq

无限分类三:递归

第一步,获取全部数据 第二部,写一个子分类的函数 第三,写一个递归循环函数 代码: /** * 查询所有数据 并且缓存 * */ public function authData(){ $res = $this->order('listorder asc')->select(); // 格式化 foreach($res as $k=>$v){ $result[$v['id']] = $v; } Cache::set('auth',$result); return $result; } /

问答项目---递归重新排序无限极子分类数组

递归重新排序无限极子分类数组方法: // 递归重新排序无限极子分类数组 function recursive($array,$pid=0,$level=0){ $arr = array(); foreach ($array as $v) { if($v['pid'] == $pid){ $v['level'] = $level; $v['html'] = str_repeat('--',$level); $arr[] = $v; $arr = array_merge($arr,recursive

Think PHP递归获取所有的子分类的ID (删除当前及子分类)

递归获取所有的子分类的ID: //递归获取所有的子分类的ID function get_all_child($array,$id){ $arr = array(); foreach($array as $v){ if($v['pid'] == $id){ $arr[] = $v['id']; $arr = array_merge($arr,get_all_child($array,$v['id'])); }; }; return $arr; } 在进行删除栏目的时候,需要删除当前栏目下的所有子栏