public function getTree(){ $data = $this->select(); $list = $this->_getTreeData($data); return $list; } private function _getTreeData($data=array(), $parent_id=0){ $childs = $this->_buildTree($data, $parent_id); //获取所有的父分类 if(empty($childs)){ return null; } foreach($childs as $key => $val){ $rescurTree = $this->_getTreeData($data, $val['id']); //递归获取当前分类下的所有子分类 if($rescurTree != null){ $childs[$key]['childs'] = $rescurTree; } } return $childs; } private function _buildTree(&$data, $parent_id){ $res = array(); foreach($data as $key => $val){ if($val['parent_id'] == $parent_id){ $res[] = $val; } } return $res; }
时间: 2024-09-23 15:00:48