这个用法只限于在一级和二级导航在一块的情况,如果是分开的,就去看另一篇无极限分类
// 递归:菜单分类显示 public function levelList($table ='mvc_category', $id = 0,$data_list = [],$level=0){ // 查询出来所有的一级 $data = Db::table($table)->where([ 'fid'=>$id ])->select(); // 组装数据 foreach($data as $k => $v){ $data_list[$v['id']]['name'] = str_repeat(' ',$level).'|-'.$v['name']; $data_list[$v['id']]['id'] = $v['id']; $data_list[$v['id']]['fid'] = $v['fid']; if(isset($v['type'])){ $data_list[$v['id']]['type'] = $v['type']; } if(isset($v['module'])){ $data_list[$v['id']]['module'] = $v['module']; } $data_list[$v['id']]['level'] = $level; $data_list = $this->levelList($table,$v['id'],$data_list,$level+1); } return $data_list; }