helpers.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. if (!function_exists('make_tree')) {
  3. /**
  4. * @param array $list
  5. * @param int $parentId
  6. * @return array
  7. */
  8. function make_tree(array $list, $parentId = 0) {
  9. $tree = [];
  10. if (empty($list)) {
  11. return $tree;
  12. }
  13. $newList = [];
  14. foreach ($list as $k => $v) {
  15. $newList[$v['id']] = $v;
  16. }
  17. foreach ($newList as $value) {
  18. if ($parentId == $value['parent_id']) {
  19. $tree[] = &$newList[$value['id']];
  20. } elseif (isset($newList[$value['parent_id']])) {
  21. $newList[$value['parent_id']]['children'][] = &$newList[$value['id']];
  22. }
  23. }
  24. return $tree;
  25. }
  26. }
  27. if (!function_exists('text_diff')) {
  28. function text_diff(string $text1, string $text2): string
  29. {
  30. $text1 = str_replace('&nbsp;', '', trim($text1));
  31. $text2 = str_replace('&nbsp;', '', trim($text2));
  32. $w = explode("\n", $text1);
  33. $o = explode("\n", $text2);
  34. $w1 = array_diff_assoc($w,$o);
  35. $o1 = array_diff_assoc($o,$w);
  36. $w2 = array();
  37. $o2 = array();
  38. foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1) . "<del>" . trim($val) . "</del>";
  39. foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1) . "<ins>" . trim($val) . "</ins>";
  40. $diff = array_merge($w2, $o2);
  41. ksort($diff);
  42. return implode("\n", $diff);
  43. }
  44. }