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;
}
实现无限极分类-递归实现
最新推荐文章于 2022-06-08 16:04:41 发布