Tree.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. namespace util;
  12. /**
  13. * 树结构生成类
  14. * @author CaiWeiMing <314013107@qq.com>
  15. */
  16. class Tree
  17. {
  18. /**
  19. * @var object 对象实例
  20. */
  21. protected static $instance;
  22. /**
  23. * 配置参数
  24. * @var array
  25. */
  26. protected static $config = [
  27. 'id' => 'id', // id名称
  28. 'pid' => 'pid', // pid名称
  29. 'title' => 'title', // 标题名称
  30. 'child' => 'child', // 子元素键名
  31. 'html' => '┝ ', // 层级标记
  32. 'step' => 4, // 层级步进数量
  33. ];
  34. /**
  35. * 架构函数
  36. * @param array $config
  37. */
  38. public function __construct($config = [])
  39. {
  40. self::$config = array_merge(self::$config, $config);
  41. }
  42. /**
  43. * 配置参数
  44. * @param array $config
  45. * @return object
  46. */
  47. public static function config($config = [])
  48. {
  49. if (!empty($config)) {
  50. $config = array_merge(self::$config, $config);
  51. }
  52. if (is_null(self::$instance)) {
  53. self::$instance = new static($config);
  54. }
  55. return self::$instance;
  56. }
  57. /**
  58. * 将数据集格式化成层次结构
  59. * @param array/object $lists 要格式化的数据集,可以是数组,也可以是对象
  60. * @param int $pid 父级id
  61. * @param int $max_level 最多返回多少层,0为不限制
  62. * @param int $curr_level 当前层数
  63. * @author 蔡伟明 <314013107@qq.com>
  64. * @return array
  65. */
  66. public static function toLayer($lists = [], $pid = 0, $max_level = 0, $curr_level = 0)
  67. {
  68. $trees = [];
  69. $lists = array_values($lists);
  70. foreach ($lists as $key => $value) {
  71. if ($value[self::$config['pid']] == $pid) {
  72. if ($max_level > 0 && $curr_level == $max_level) {
  73. return $trees;
  74. }
  75. unset($lists[$key]);
  76. $child = self::toLayer($lists, $value[self::$config['id']], $max_level, $curr_level + 1);
  77. if (!empty($child)) {
  78. $value[self::$config['child']] = $child;
  79. }
  80. $trees[] = $value;
  81. }
  82. }
  83. return $trees;
  84. }
  85. /**
  86. * 将数据集格式化成列表结构
  87. * @param array|object $lists 要格式化的数据集,可以是数组,也可以是对象
  88. * @param integer $pid 父级id
  89. * @param integer $level 级别
  90. * @return array 列表结构(一维数组)
  91. */
  92. public static function toList($lists = [], $pid = 0, $level = 0)
  93. {
  94. if (is_array($lists)) {
  95. $trees = [];
  96. foreach ($lists as $key => $value) {
  97. if ($value[self::$config['pid']] == $pid) {
  98. $title_prefix = str_repeat("&nbsp;", $level * self::$config['step']).self::$config['html'];
  99. $value['level'] = $level + 1;
  100. $value['title_prefix'] = $level == 0 ? '' : $title_prefix;
  101. $value['title_display'] = $level == 0 ? $value[self::$config['title']] : $title_prefix.$value[self::$config['title']];
  102. $trees[] = $value;
  103. unset($lists[$key]);
  104. $trees = array_merge($trees, self::toList($lists, $value[self::$config['id']], $level + 1));
  105. }
  106. }
  107. return $trees;
  108. } else {
  109. foreach ($lists as $key => $value) {
  110. if ($value[self::$config['pid']] == $pid && is_object($value)) {
  111. $title_prefix = str_repeat("&nbsp;", $level * self::$config['step']).self::$config['html'];
  112. $value['level'] = $level + 1;
  113. $value['title_prefix'] = $level == 0 ? '' : $title_prefix;
  114. $value['title_display'] = $level == 0 ? $value[self::$config['title']] : $title_prefix.$value[self::$config['title']];
  115. $lists->offsetUnset($key);
  116. $lists[] = $value;
  117. self::toList($lists, $value[self::$config['id']], $level + 1);
  118. }
  119. }
  120. return $lists;
  121. }
  122. }
  123. /**
  124. * 根据子节点返回所有父节点
  125. * @param array $lists 数据集
  126. * @param string $id 子节点id
  127. * @return array
  128. */
  129. public static function getParents($lists = [], $id = '')
  130. {
  131. $trees = [];
  132. foreach ($lists as $value) {
  133. if ($value[self::$config['id']] == $id) {
  134. $trees[] = $value;
  135. $trees = array_merge(self::getParents($lists, $value[self::$config['pid']]), $trees);
  136. }
  137. }
  138. return $trees;
  139. }
  140. /**
  141. * 获取所有子节点id
  142. * @param array $lists 数据集
  143. * @param string $pid 父级id
  144. * @return array
  145. */
  146. public static function getChildsId($lists = [], $pid = '')
  147. {
  148. $result = [];
  149. foreach ($lists as $value) {
  150. if ($value[self::$config['pid']] == $pid) {
  151. $result[] = $value[self::$config['id']];
  152. $result = array_merge($result, self::getChildsId($lists, $value[self::$config['id']]));
  153. }
  154. }
  155. return $result;
  156. }
  157. /**
  158. * 获取所有子节点
  159. * @param array $lists 数据集
  160. * @param string $pid 父级id
  161. * @return array
  162. */
  163. public static function getChilds($lists = [], $pid = '')
  164. {
  165. $result = [];
  166. foreach ($lists as $value) {
  167. if ($value[self::$config['pid']] == $pid) {
  168. $result[] = $value;
  169. $result = array_merge($result, self::getChilds($lists, $value[self::$config['id']]));
  170. }
  171. }
  172. return $result;
  173. }
  174. }