凌的博客

您现在的位置是: 首页 > 学无止境 > PHP > 

PHP

php tree树形结构类

2015-04-25 PHP 817
/** 树形结构类 */ class Tree{ public $categorys; public $id; public $pid; public $name; /** 初始化 @param array $categorys 分类数组 @param string $id @param string $pid @param string $
/**
树形结构类
*/
class Tree{
	
	public $categorys;
	public $id;
	public $pid;
	public $name;
	/**
	初始化
	@param array $categorys 分类数组
	@param string $id 
	@param string $pid 
	@param string $name 
	*/
	public function __construct($categorys,$id='id',$pid='pid',$name='name'){
		$this->categorys  = $categorys;
		$this->id         = $id;
		$this->pid        = $pid;
		$this->name       = $name;
	}
	
	/**
	获取所有子级
	@param int $catid 分类id
	@param array $childs 子分类
	*/
	public function getAllChild($catid=0,&$childs = array()){
		if(!empty($this->categorys)){
			foreach($this->categorys as $key=>$value){
				if($value[$this->pid] == $catid){
					$childs[] = $value;
					$this->getAllChild($value[$this->id],$childs);
				}
			}
		}
		return $childs;
	}
	
	/**
	获取 子级分类
	@param int $catid 分类id
	@param array $childs 子分类
	*/
	public function getChild($catid=0){
		if(!empty($this->categorys)){
			foreach($this->categorys as $key=>$value){
				if($value[$this->pid] == $catid){
					$childs[] = $value;
				}
			}
		}
		return $childs;
	}
	
	/**
	获取所有子级id
	@param int $catid 分类id
	*/
	public function getChildIds($catid=0){
		$childs = $this->getAllChild($catid);
		$ids = array();
		if(!empty($childs)){
			foreach($childs as $v){
				$ids[] = $v[$this->id];	
			}	
		}
		return $ids;
	}
	
	/**
	获取树形结构数据
	@param int $catid 分类id
	@param string $child 子类键值
	@param int $level 递归层级
	@param int $max_level 最深层级
	*/
	public function getTree($catid=0,$child='child',&$level=0,$max_level=5){
		$current_cate  = array();
		if(!empty($this->categorys)){
			foreach($this->categorys as $key=>$value){
				if($value[$this->pid] == $catid){
					$current_cate[] = $value;
				}
			}
		}
		$cates = array();
		if(!empty($current_cate)){
			if($level++ > $max_level){
				return $cates;	
			}
			foreach($current_cate as $k=>$v){
				$v[$child] = $this->getTree($v[$this->id],$child,$level,$max_level);
				$cates[] = $v;
			}	
			return $cates;
		}
	}
	/**
	获取树形 二维数组
	@param int $catid 分类id
	@param int $pid 顶级id
	@param int $space 间隔符
	*/
	public function getTreeSelect($catid,$pid=0,$space='--'){
		$tree = $this->getTree($catid);
		return $this->getTreeList($tree,$pid,$space);
	}

	/**
	获取树形 二维数组
	@param int $catid 分类id
	@param int $pid 顶级id
	@param string $lists 树形列表
	@param int $space 间隔符
	*/
	public function getTreeList($tree,$pid=0,$space='--',&$lists=array()){
		if(!empty($tree)){
			$cate = array();
			foreach($tree as $key => $value){
				$cate = $this->getCate($value[$this->id]);
				$cate['title_show'] = str_repeat($space,$this->getLevel($cate['id'],$pid)-1);
				$lists[] = $cate;
				if(!empty($value['child'])){
					$this->getTreeList($value['child'],$pid,$space,$lists);	
				}
			}
		}
		return $lists;
	}
	
	/**
	获取上一级
	@param int $catid 分类id
	*/
	public function getParent($catid=0){
		$cate = $this->getCate($catid);
		return $this->getCate($cate[$this->pid]);
	}
	
	/**
	获取所有上一级
	@param int $catid 分类id
	*/
	public function getAllParent($catid=0,$pid=0,&$cates=array()){
		$cates[] = $cate = $this->getCate($catid);
		if($cate[$this->pid] == $pid){
			return $cates;
		}else{
			return $this->getAllParent($cate[$this->pid],$pid,$cates);	
		}
	}
	
	/**
	获取当前分类的层级数
	@param int $catid 分类id
	@param int $pid 上级id
	*/
	public function getLevel($catid,$pid=0){
		return count($this->getAllParent($catid,$pid));
	}
	
	/**
	获取分类
	@param int $catid 分类id
	*/
	public function getCate($catid){
		$cate = array();
		if(!empty($this->categorys)){
			foreach($this->categorys as $key=>$value){
				if($value[$this->id] == $catid){
					$cate  = $value;
					break;
				}
			}
		}
		return $cate;
	}
	
}

文章评论

0条评论