Tree.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace ba;
  3. /**
  4. * 树
  5. */
  6. class Tree
  7. {
  8. /**
  9. * @var Tree
  10. */
  11. protected static $instance;
  12. /**
  13. * 生成树型结构所需修饰符号
  14. * @var array
  15. */
  16. public static $icon = array('│', '├', '└');
  17. /**
  18. * 子级数据(树枝)
  19. * @var array
  20. */
  21. protected $childrens = [];
  22. /**
  23. * 初始化
  24. * @access public
  25. * @return Tree
  26. */
  27. public static function instance(): Tree
  28. {
  29. if (is_null(self::$instance)) {
  30. self::$instance = new static();
  31. }
  32. return self::$instance;
  33. }
  34. /**
  35. * 将数组某个字段渲染为树状,需自备children children可通过$this->assembleChild()方法组装
  36. * @param array $arr 要改为树状的数组
  37. * @param string $field '树枝'字段
  38. * @param int $level 递归数组层次,无需手动维护
  39. * @param false $superiorEnd 递归上一级树枝是否结束,无需手动维护
  40. * @return array
  41. */
  42. public static function getTreeArray(array $arr, string $field = 'name', int $level = 0, bool $superiorEnd = false): array
  43. {
  44. $level++;
  45. $number = 1;
  46. $total = count($arr);
  47. foreach ($arr as $key => $item) {
  48. $prefix = ($number == $total) ? self::$icon[2] : self::$icon[1];
  49. if ($level == 2) {
  50. $arr[$key][$field] = str_pad('', 4) . $prefix . $item[$field];
  51. } elseif ($level >= 3) {
  52. $arr[$key][$field] = str_pad('', 4) . ($superiorEnd ? '' : self::$icon[0]) . str_pad('', ($level - 2) * 4) . $prefix . $item[$field];
  53. }
  54. if (isset($item['children']) && $item['children']) {
  55. $arr[$key]['children'] = self::getTreeArray($item['children'], $field, $level, $number == $total);
  56. }
  57. $number++;
  58. }
  59. return $arr;
  60. }
  61. /**
  62. * 递归合并树状数组(根据children多维变二维方便渲染)
  63. * @param array $data 要合并的数组 ['id' => 1, 'pid' => 0, 'title' => '标题1', 'children' => ['id' => 2, 'pid' => 1, 'title' => ' └标题1-1']]
  64. * @return array [['id' => 1, 'pid' => 0, 'title' => '标题1'], ['id' => 2, 'pid' => 1, 'title' => ' └标题1-1']]
  65. */
  66. public static function assembleTree(array $data): array
  67. {
  68. $arr = [];
  69. foreach ($data as $v) {
  70. $children = $v['children'] ?? [];
  71. unset($v['children']);
  72. $arr[] = $v;
  73. if ($children) {
  74. $arr = array_merge($arr, self::assembleTree($children));
  75. }
  76. }
  77. return $arr;
  78. }
  79. /**
  80. * 递归的根据指定字段组装 children 数组
  81. * @param array $data 数据源 例如:[['id' => 1, 'pid' => 0, title => '标题1'], ['id' => 2, 'pid' => 1, title => '标题1-1']]
  82. * @param string $pid 存储上级id的字段
  83. * @param string $pk 主键字段
  84. * @return array ['id' => 1, 'pid' => 0, 'title' => '标题1', 'children' => ['id' => 2, 'pid' => 1, 'title' => '标题1-1']]
  85. */
  86. public function assembleChild(array $data, string $pid = 'pid', string $pk = 'id'): array
  87. {
  88. if (!$data) return [];
  89. $pks = [];
  90. $topLevelData = []; // 顶级数据
  91. $this->childrens = []; // 置空子级数据
  92. foreach ($data as $item) {
  93. $pks[] = $item[$pk];
  94. // 以pid组成children
  95. $this->childrens[$item[$pid]][] = $item;
  96. }
  97. // 上级不存在的就是顶级,只获取它们的 children
  98. foreach ($data as $item) {
  99. if (!in_array($item[$pid], $pks)) {
  100. $topLevelData[] = $item;
  101. }
  102. }
  103. if (count($this->childrens) > 0) {
  104. foreach ($topLevelData as $key => $item) {
  105. $topLevelData[$key]['children'] = $this->getChildren($this->childrens[$item[$pk]] ?? [], $pk);
  106. }
  107. return $topLevelData;
  108. } else {
  109. return $data;
  110. }
  111. }
  112. /**
  113. * 获取 children 数组
  114. * 辅助 assembleChild 组装 children
  115. * @param array $data
  116. * @param string $pk
  117. * @return array
  118. */
  119. protected function getChildren(array $data, string $pk = 'id'): array
  120. {
  121. if (!$data) return [];
  122. foreach ($data as $key => $item) {
  123. if (array_key_exists($item[$pk], $this->childrens)) {
  124. $data[$key]['children'] = $this->getChildren($this->childrens[$item[$pk]], $pk);
  125. }
  126. }
  127. return $data;
  128. }
  129. }