无限级分类主要救赎采用递归算法和特殊的数据表设计实现的

我的代码

/**
      * 获取顺序的菜单信息
      * 使用递归方法
      */
     public function getHierarchicalMenus($pid , $condition=array() , $cid = 0){
         $data = array();
         if (!isset($condition['status'])){
             $condition['status'] = array('neq' , -1);
         }
         $condition['parentid'] = $pid;
         $menus = $this->_db->where($condition)->order("listorder desc")->select();
         if ($menus){
             foreach ($menus as $v){
                 $res = $this->getHierarchicalMenus($v['menu_id'] ,$condition ,$cid+1);
                 if ($res){
                     $v['child'] = $res;
                     $v['cid'] = $cid;
                 }else{
                     $v['child'] = 0;
                 }
                 $v['cid'] = $cid;
                 $data[] = $v;
             }
         }
         return $data;
     }

老师的代码

public function getTree()
     {
         $data = $this->select();
         return $this->_reSort($data);
     }
     private function _reSort($data, $parent_id=0, $level=0, $isClear=TRUE)
     {
         static $ret = array();
         if($isClear)
             $ret = array();
         foreach ($data as $k => $v)
         {
             if($v['parent_id'] == $parent_id)
             {
                 $v['level'] = $level;
                 $ret[] = $v;
                 $this->_reSort($data, $v['id'], $level+1, FALSE);
             }
         }
         return $ret;
     }

明显不同,我设计的无限级是使用array进行嵌套,那么前台的显示就有很大的问题了,

嵌套几层相应的显示就要几层判断加循环,然而老师的使用的是二维array,通过level

来判断层级,顺序由于持续深入不变,同时在递归之前先将全部数据先记录下来,减少

数据库操作次数,提高数据库并发操作。