php树形结构数据存取实例类

  1. <?php
  2. /**
  3. * Tanphp framework
  4. *
  5. *
  6. * @category Tanphp
  7. * @package Data_structure
  8. * @copyright Copyright (c) 2012 谭博 tanbo.name
  9. * @version $Id: Tree.php 25024 2012-11-26 22:22:22 tanbo $
  10. */
  11. /**
  12. * 树形结构数据存取类
  13. *
  14. * 用于对树形结构数据进行快速的存取
  15. *
  16. * @param array $arr 参数必须为标准的二维数组,包含索引字段(id)与表示树形结构的字段(path),如example中所示
  17. *
  18. * @example <code>
  19. * $arr = array(
  20. * array( 'id' => 1, 'name' => 'php', 'path' => '1' ),
  21. * array( 'id' => 3, 'name' => 'php1', 'path' => '1-3' ),
  22. * array( 'id' => 2, 'name' => 'mysql', 'path' => '2' ),
  23. * array( 'id' => 6, 'name' => 'mysql1', 'path' => '2-6' ),
  24. * array( 'id' => 7, 'name' => 'mysql2', 'path' => '2-7' ),
  25. * array( 'id' => 5, 'name' => 'php11', 'path' => '1-3-5' ),
  26. * array( 'id' => 4, 'name' => 'php2', 'path' => '1-4' ),
  27. * );
  28. * $cate = new Tree($arr);
  29. *
  30. * $data = $cate->getChild(2);
  31. *
  32. * print_r($data->toArray());
  33. * </code>
  34. *
  35. */
  36. class Tree
  37. {
  38. public $_info; //节点信息
  39. public $_child = array(); //子节点
  40. private $_parent; //父节点
  41. private $_data; //当前操作的临时数据
  42. private static $_indexs = array(); //所有节点的索引
  43. private static $_index_key = 'id'; //索引键
  44. private static $_tree_key = 'path'; //树形结构表达键
  45. private static $_tree_delimiter = '-'; //属性结构表达分割符
  46. /**
  47. * 构造函数
  48. *
  49. * @param array $arr
  50. * @param boole $force_sort 如果为真,将会强制对$arr 进行排序
  51. * @return void
  52. */
  53. public function __construct(array $arr = array(), $force_sort=true)
  54. {
  55. if ($force_sort === true) {
  56. $arr=$this->_array_sort($arr, self::$_tree_key);
  57. }
  58. if (!emptyempty($arr)) {
  59. $this->_init($arr);
  60. }
  61. }
  62. /**
  63. * 初始存储树形数据
  64. *
  65. * @param array $arr
  66. * @return void
  67. */
  68. private function _init(array $arr)
  69. {
  70. foreach ($arr as $item) {
  71. $path = $item[self::$_tree_key];
  72. $paths = explode(self::$_tree_delimiter, $path);
  73. $count_paths = count($paths);
  74. $parent_id = isset($paths[$count_paths-2]) ? $paths[$count_paths-2] : NULL;
  75. if ( $count_paths>1 //如果有父级
  76. && array_key_exists($parent_id, self::$_indexs) //父级已经被存入索引
  77. && self::$_indexs[$parent_id] instanceof Tree //父级为Tree对象
  78. ) {
  79. self::$_indexs[$parent_id]->addChild($item);
  80. } elseif ($count_paths == 1) {
  81. $this->addChild($item);
  82. } else {
  83. throw new Exception("path数据错误".var_export($item, true));
  84. }
  85. }
  86. //print_r(self::$_indexs);
  87. }
  88. /**
  89. * 添加子节点
  90. *
  91. * @param array $item
  92. * @return void
  93. */
  94. public function addChild(array $item, $parent = NULL)
  95. {
  96. $child = new Tree();
  97. $child->_info = $item;
  98. $child->_parent = $parent == NULL ? $this : $parent;
  99. $child->_parent->_child[] = $child;
  100. $this->_addIndex($item, $child->_getSelf());
  101. }
  102. /**
  103. * 添加节点到索引
  104. *
  105. * @param array $item
  106. * @param mix $value
  107. * @return void
  108. */
  109. private function _addIndex(array $item, $value)
  110. {
  111. if (array_key_exists(self::$_index_key, $item) && is_int($item[self::$_index_key])) {
  112. self::$_indexs[$item[self::$_index_key]] = $value;
  113. } else {
  114. throw new Exception("id字段不存在或者不为字符串");
  115. }
  116. }
  117. /**
  118. * 获取对自己的引用
  119. *
  120. * @return Tree object quote
  121. */
  122. private function _getSelf()
  123. {
  124. return $this;
  125. }
  126. /**
  127. * 获取指定id的节点的子节点
  128. *
  129. * @param int $id
  130. * @return Tree object
  131. */
  132. public function getChild($id)
  133. {
  134. $data = self::$_indexs[$id]->_child;
  135. $this->_data = $data;
  136. return $this;
  137. }
  138. /**
  139. * 获取指定id的节点的父节点
  140. *
  141. * @param int $id
  142. * @return Tree object
  143. */
  144. public function getParent($id)
  145. {
  146. $data = self::$_indexs[$id]->_parent;
  147. $this->_data = $data;
  148. return $this;
  149. }
  150. /**
  151. * 获取指定id的节点的同级节点
  152. *
  153. * @param int $id
  154. * @return Tree object
  155. */
  156. public function getBrother($id)
  157. {
  158. $data = self::$_indexs[$id]->_parent->_child;
  159. $this->_data = $data;
  160. return $this;
  161. }
  162. /**
  163. * 将Tree对象转化为数组
  164. *
  165. * @param object $object
  166. * @return array
  167. */
  168. public function toArray($obj = NULL)
  169. {
  170. $obj = ($obj === NULL) ? $this->_data : $obj;
  171. $arr = array();
  172. $_arr = is_object($obj) ? $this->_getBaseInfo($obj) : $obj;
  173. if (is_array($_arr)) {
  174. foreach ($_arr as $key => $val){
  175. $val = (is_array($val) || is_object($val)) ? $this->toArray($val) : $val;
  176. $arr[$key] = $val;
  177. }
  178. } else {
  179. throw new Exception("_arr不是数组");
  180. }
  181. return $arr;
  182. }
  183. /**
  184. * 过滤_parent等字段,以免造成无限循环
  185. *
  186. * @param object $obj
  187. * @return void
  188. */
  189. private function _getBaseInfo($obj)
  190. {
  191. $vars = get_object_vars($obj);
  192. $baseInfo['_info'] = $vars['_info'];
  193. $baseInfo['_child'] = $vars['_child'];
  194. return $baseInfo;
  195. }
  196. /**
  197. * 二维数组排序
  198. *
  199. * 根据指定的键名对二维数组进行升序或者降序排列
  200. *
  201. * @param array $arr 二维数组
  202. * @param string $keys
  203. * @param string $type 必须为 asc或desc
  204. * @throws 当参数非法时抛出异常
  205. * @return 返回排序好的数组
  206. */
  207. private function _array_sort(array $arr, $keys, $type = 'asc') {
  208. if (!is_string($keys)) {
  209. throw new Exception("非法参数keys:参数keys的类型必须为字符串");
  210. }
  211. $keysvalue = $new_array = array();
  212. foreach ($arr as $k=>$v) {
  213. if (!is_array($v) || !isset($v[$keys])) {
  214. throw new Exception("参数arr不是二维数组或arr子元素中不存在键'{$keys}'");
  215. }
  216. $keysvalue[$k] = $v[$keys];
  217. }
  218. switch ($type) {
  219. case 'asc':
  220. asort($keysvalue);
  221. break;
  222. case 'desc':
  223. arsort($keysvalue);
  224. break;
  225. default:
  226. throw new Exception("非法参数type :参数type的值必须为 'asc' 或 'desc'");
  227. }
  228. reset($keysvalue);
  229. foreach ($keysvalue as $k=>$v) {
  230. $new_array[$k] = $arr[$k];
  231. }
  232. return $new_array;
  233. }
  234. }
  235. ?>