• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

php 树形结构的读取

ECStore 水墨上仙 2615次浏览

php 树形结构的读取

<?php // 从数组转换,操作无限分类数组. // 数组格式, array(array('cid', 'pid', 'val'), array('cid', 'pid', 'val')); class tree_array { var $data = array(); var $child = array(); var $layer = array(); var $parent = array(); // $array, 被操纵的数组, $cid 分类 id 的key. $pid 父分类 id 的key. $value 数据的 key function tree_array($array = array(), $cid = 'cid', $pid = 'pid', $value = null) { if(!is_array($array)) return false; foreach($array as $v) { if(isset($v[$value])) { $this->setNode($v[$cid], $v[$pid], $v[$value]);
			}
			else
			{
				$this->setNode($v[$cid], $v[$pid], $v);
			}
		}
	}
	function setNode($id, $parent, $value){
		$parent = $parent ? $parent : 0;
		$this->data[$id] = $value;
		// if(!isset($this->child[$id])) $this->child[$id] = array();
		if(!isset($this->child[$parent])) $this->child[$parent] = array();
		$this->child[$parent][] = $id;
		$this->parent[$id] = $parent;
	}

	function getValue($id) {
		if(!isset($this->data[$id])) return false;
		return $this->data[$id];
	}

	function getLayer($id, $space = false) {
		if(!isset($this->parent[$id])) return false;
		$layer = count($this->getParents($id)) + 1;
		return $space ? str_repeat($space, $layer) : $layer;
	}

	function getTreeList(&$tree, $root= 0, $space=null) {
		if(!isset($this->child[$root])) return false;
		foreach($this->child[$root] as $key=>$id) {
			if($space) {
				$tree[$id] = $this->getLayer($id, $space) . $this->data[$id];
			}
			else
			{
				$tree[$id] = $this->data[$id];
			}
			if(isset($this->child[$id])) $this->getTreeList($tree, $id, $space);
		}
	}

	function getParent($id) {
		if(!isset($this->parent[$id])) return false;
		$tid = $this->parent[$id];
        if(!$tid) return 0;
		return array($tid => $this->data[$tid]);
	}

	function getParents($id) {
		if(!isset($this->parent[$id])) return false;
		$parents = array();
		while($this->parent[$id]){
			$id = $this->parent[$id];
			$parents[$id] = $this->data[$id];
		}
		return $parents;
	}
	function getChild($id) {
		if(!isset($this->child[$id])) return false;
		$array = array();
		foreach($this->child[$id] as $v) {
			$array[$v] = $this->data[$v];
		}
		return $array;
	}

	function getChilds($id = 0) {
		if(!isset($this->child[$id])) return false;
		$child = array();
		$this->getTreeList($child, $id);
		return $child;
	}

	function html_options($id = 0, $space=' ',$layer=0) {
		static $layer;
		if(!isset($this->child[$id])) return false;
		$tree = array();
		foreach($this->child[$id] as $key=>$id) {
			if($space) {
				$tree[$id] =(str_repeat($space, $layer)) . $this->data[$id];
			}
			else
			{
				$tree[$id] = $this->data[$id];
			}
			if(isset($this->child[$id])) {
				$layer++;
				$tree += $this->html_options($id, $space,$layer);
				$layer--;
			}
		}
		return $tree;
	}
}

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明php 树形结构的读取
喜欢 (0)
加载中……