Tree.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace utils;
  3. class Tree
  4. {
  5. public function createTreeByList($data, $pid = 0, $lv = 1, $idField = 'id', $pidField = 'pid', $child = 'tcd')
  6. {
  7. $tree = [];
  8. foreach ($data as $k => $v) {
  9. if ($v[$pidField] == $pid) {
  10. $v['tlv'] = $lv;
  11. $v[$child] = [];
  12. $tcd = $this->createTreeByList($data, $v[$idField], $lv + 1, $idField, $pidField, $child);
  13. if (!empty($tcd)) {
  14. $v[$child] = $tcd;
  15. }
  16. $tree[] = $v;
  17. }
  18. }
  19. return $tree;
  20. }
  21. public function treeToList($tree, $ignore_tlv = 0)
  22. {
  23. $list = [];
  24. foreach ($tree as $k => $v) {
  25. if ($ignore_tlv == $v['tlv']) {
  26. $list[] = $v;
  27. continue;
  28. }
  29. $tcd = empty($v['tcd']) ? [] : $this->treeToList($v['tcd'], $ignore_tlv);
  30. if (isset($v['tcd'])) {
  31. unset($v['tcd']);
  32. }
  33. $list[] = $v;
  34. foreach ($tcd as $child) {
  35. $list[] = $child;
  36. }
  37. }
  38. return $list;
  39. }
  40. public function createListByTree($tree, $name = 'name', $ignore_pid = -1, $prefix = '')
  41. {
  42. $list = [];
  43. $count = count($tree);
  44. foreach ($tree as $k => $v) {
  45. //名称前缀
  46. if ($ignore_pid != $v['pid']) {
  47. if ($k < $count - 1) {
  48. $v[$name] = $prefix . ' ├ ' . $v[$name];
  49. $tcd = empty($v['tcd']) ? [] : $this->createListByTree($v['tcd'], $name, $ignore_pid, $prefix . ' │ ');
  50. } else {
  51. $v[$name] = $prefix . ' ┕ ' . $v[$name];
  52. $tcd = empty($v['tcd']) ? [] : $this->createListByTree($v['tcd'], $name, $ignore_pid, $prefix . '   ');
  53. }
  54. } else {
  55. $v[$name] = $prefix . $v[$name];
  56. $tcd = empty($v['tcd']) ? [] : $this->createListByTree($v['tcd'], $name, $ignore_pid, $prefix);
  57. }
  58. if (isset($v['tcd'])) {
  59. unset($v['tcd']);
  60. }
  61. $v['tcd'] = count($tcd);
  62. $list[] = $v;
  63. foreach ($tcd as $child) {
  64. $list[] = $child;
  65. }
  66. }
  67. return $list;
  68. }
  69. //将数组保存文件
  70. public function saveArrayToFile($file, $array, $topNote = '')
  71. {
  72. $str = $this->arrayToString($array);
  73. $this->folders(dirname($file));
  74. if ($topNote) {
  75. $topNote = "/*" . $topNote . "*/\n\n";
  76. }
  77. return file_put_contents($file, "<?php \n" . $topNote . "return " . $str . ";");
  78. }
  79. private function arrayToString($array, $ref = '')
  80. {
  81. if (empty($array)) {
  82. return '[]';
  83. }
  84. $nowrap = true;
  85. $count = count($array);
  86. if ($count <= 8) {
  87. foreach ($array as $k => $v) {
  88. if (empty($v)) {
  89. continue;
  90. }
  91. if (!is_scalar($v)) {
  92. $nowrap = false;
  93. break;
  94. }
  95. if (is_string($k) && mb_strlen($k, 'utf-8') > 20) {
  96. $nowrap = false;
  97. break;
  98. }
  99. if (is_string($v) && mb_strlen($v, 'utf-8') > 60) {
  100. $nowrap = false;
  101. break;
  102. }
  103. }
  104. } else {
  105. $nowrap = false;
  106. }
  107. $str = $nowrap ? "[" : "[\n";
  108. $newref = $nowrap ? '' : $ref . ' ';
  109. $i = 0;
  110. foreach ($array as $k => $v) {
  111. //key
  112. if (is_string($k)) {
  113. $str .= "$newref'$k' => ";
  114. } else {
  115. $str .= "{$newref}{$k} => ";
  116. }
  117. //value
  118. if (is_array($v)) {
  119. $str .= self::arrayToString($v, $ref . ' ');
  120. } elseif (is_int($v) || is_float($v)) {
  121. $str .= $v;
  122. } elseif (is_string($v)) {
  123. $str .= "'$v'";
  124. } elseif (is_bool($v)) {
  125. $str .= ($v ? 'true' : 'false');
  126. } else {
  127. //对象略
  128. $str .= "''";
  129. }
  130. if ($i == $count - 1) {
  131. $str .= $nowrap ? "" : "\n";
  132. } else {
  133. $str .= $nowrap ? ", " : ",\n";
  134. }
  135. $i++;
  136. }
  137. $str .= $nowrap ? "]" : $ref . "]";
  138. return $str;
  139. }
  140. /**
  141. * 创建文件夹
  142. * @param $dir
  143. * @return bool
  144. */
  145. public function folders($dir)
  146. {
  147. return is_dir($dir) || ($this->folders(dirname($dir)) && mkdir($dir));
  148. }
  149. }