php递归获取无限分类菜单

从数据库获取所有菜单信息,需要根据id,pid字段获取主菜单及其子菜单,以及子菜单下的子菜单,可以通过函数递归来实现。

<?php 
class Menu {
    public $menu = array(
      array('id'=>1,  'title'=>"一级1",'pid'=>0),
     array('id'=>2,  'title'=>"一级2",'pid'=>0),
     array('id'=>3,  'title'=>"二级11",'pid'=>1),
     array('id'=>4,  'title'=>"二级12",'pid'=>1),
     array('id'=>5,  'title'=>"二级21",'pid'=>2),
        array('id'=>6,  'title'=>"二级22",'pid'=>2),
     array('id'=>7,  'title'=>"三级1",'pid'=>3),
     array('id'=>8,  'title'=>"三级2",'pid'=>3),
       );

    public function __construct() {
         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(2) {
  [0] => array(4) {
    ["id"] => int(1)
    ["title"] => string(7) "一级1"
    ["pid"] => int(0)
    ["child"] => array(2) {
      [0] => array(4) {
        ["id"] => int(3)
        ["title"] => string(8) "二级11"
        ["pid"] => int(1)
        ["child"] => array(2) {
          [0] => array(3) {
            ["id"] => int(7)
            ["title"] => string(7) "三级1"
            ["pid"] => int(3)
          }
          [1] => array(3) {
            ["id"] => int(8)
            ["title"] => string(7) "三级2"
            ["pid"] => int(3)
          }
        }
      }
      [1] => array(3) {
        ["id"] => int(4)
        ["title"] => string(8) "二级12"
        ["pid"] => int(1)
      }
    }
  }
  [1] => array(4) {
    ["id"] => int(2)
    ["title"] => string(7) "一级2"
    ["pid"] => int(0)
    ["child"] => array(2) {
      [0] => array(3) {
        ["id"] => int(5)
        ["title"] => string(8) "二级21"
        ["pid"] => int(2)
      }
      [1] => array(3) {
        ["id"] => int(6)
        ["title"] => string(8) "二级22"
        ["pid"] => int(2)
      }
    }
  }
}

posted on 2014-11-22 21:08  超大杯摩卡星冰乐  阅读(1782)  评论(0编辑  收藏  举报

导航