123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- if (!function_exists('make_tree')) {
- /**
- * @param array $list
- * @param int $parentId
- * @return array
- */
- function make_tree(array $list, $parentId = 0) {
- $tree = [];
- if (empty($list)) {
- return $tree;
- }
- $newList = [];
- foreach ($list as $k => $v) {
- $newList[$v['id']] = $v;
- }
- foreach ($newList as $value) {
- if ($parentId == $value['parent_id']) {
- $tree[] = &$newList[$value['id']];
- } elseif (isset($newList[$value['parent_id']])) {
- $newList[$value['parent_id']]['children'][] = &$newList[$value['id']];
- }
- }
- return $tree;
- }
- }
- if (!function_exists('text_diff')) {
- function text_diff(string $text1, string $text2): string
- {
- $text1 = str_replace(' ', '', trim($text1));
- $text2 = str_replace(' ', '', trim($text2));
- $w = explode("\n", $text1);
- $o = explode("\n", $text2);
- $w1 = array_diff_assoc($w,$o);
- $o1 = array_diff_assoc($o,$w);
- $w2 = array();
- $o2 = array();
- foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1) . "<del>" . trim($val) . "</del>";
- foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1) . "<ins>" . trim($val) . "</ins>";
- $diff = array_merge($w2, $o2);
- ksort($diff);
- return implode("\n", $diff);
- }
- }
|