Tree.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\model;
  3. class Tree
  4. {
  5. public static function title($data, $pid, $before = "")
  6. {
  7. $arr = [];
  8. if (empty($before)) {
  9. $before = "├ ";
  10. } else {
  11. $before = "│ " . $before;
  12. }
  13. if (!empty($data)) {
  14. foreach ($data as $iv) {
  15. if ($iv['pid'] == $pid) {
  16. if ($before == "├ ") {
  17. } else {
  18. $iv['title'] = $before . $iv['title'];
  19. }
  20. $arr[] = $iv;
  21. $arr = array_merge($arr, self::title($data, $iv["id"], $before));
  22. }
  23. }
  24. }
  25. $before = "";
  26. return $arr;
  27. }
  28. public static function listarray($data, $pid, $before = "")
  29. {
  30. $arr = [];
  31. if (!empty($data)) {
  32. foreach ($data as $iv) {
  33. if ($iv['pid'] == $pid) {
  34. $arr[$iv['id']] = $iv;
  35. $arr[$iv['id']]['son'] = self::listarray($data, $iv["id"], $before);
  36. }
  37. }
  38. }
  39. return $arr;
  40. }
  41. public static function treelist($data, $pid, $before = "")
  42. {
  43. $arr = [];
  44. if (!empty($data)) {
  45. foreach ($data as $key => $iv) {
  46. if ($iv['pid'] == $pid) {
  47. $arr[$key] = $iv;
  48. $arr[$key]['son'] = self::treelist($data, $iv["id"], $before);
  49. }
  50. }
  51. }
  52. //var_dump($arr);
  53. return $arr;
  54. }
  55. }