$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) . "" . trim($val) . ""; foreach ($o1 as $idx => $val) $o2[sprintf("%03d>", $idx)] = sprintf("%03d+ ", $idx + 1) . "" . trim($val) . ""; $diff = array_merge($w2, $o2); ksort($diff); return implode("\n", $diff); } } if (!function_exists('throw_validation_if')) { function throw_validation_if($condition, ...$parameters) { return throw_if($condition, \App\Exceptions\ValidationException::class, ...$parameters); } } if (!function_exists('make_array_list')) { function make_array_list(string $value): array { $array = []; if ($value == '') { return $array; } $array = explode(',', $value); $array = array_filter($array, function ($value) { return $value !== ''; }); $array = array_map(function ($value) { return intval($value); }, $array); return ($array); } } if (!function_exists('json_decode_arr')) { /** * 让json decode统一返回array * @param mixed $string * @return array */ function json_decode_arr($string): array { $arr = json_decode($string, true); if (is_null($arr)) { $arr = []; } return $arr; } } if (!function_exists('cos_upload_prefix')) { /** * 返回cos上传路径前缀 * @param int $companyId * @param string $objectType * @param mixed $extra * @return string */ function cos_upload_prefix(int $companyId = 0, string $objectType = '', string $extra = ''): string { if (empty($extra)) { $extra = date("Ymd/Hi"); } return sprintf("c%s/%s/%s", $companyId, $objectType, $extra); } } if (!function_exists('date_time_str')) { /** * 生成日期时间格式 * @param int $companyId * @param string $objectType * @param mixed $extra * @return string */ function date_time_str(int $timestamp = 0): string { if ($timestamp == 0) { $timestamp = time(); } return date('Y-m-d H:i:s', $timestamp); } } //对树进行id重置封装生成新的id if (!function_exists('make_tree_display_id')) { function make_tree_display_id(&$tree, $parentId = 0, $index = 1, $isParent = true) { foreach ($tree as &$node) { // 设置当前节点的ID $node['display_id'] = ($parentId == 0 || $isParent == true) ? (string) $index : $parentId . '.' . $index; // 更新索引值 $index++; // 如果当前节点有子节点,递归调用本函数,并传递新的父ID和索引值 if (isset($node['children'])) { make_tree_display_id($node['children'], $node['display_id'], 1, false); } } } } //对普通列表装生成新的id if (!function_exists('make_display_id')) { function make_display_id($datas, $pageSize) { $page = $datas->currentPage(); $newId = ($page - 1) * $pageSize + 1; foreach ($datas as $data) { $data->display_id = $newId++; } } } if (!function_exists('array_are_equal')) { function array_are_equal(array $array1, array $array2): bool { if (array_keys($array1) !== array_keys($array2)) { return false; } foreach ($array1 as $key => $value) { // 如果当前元素是数组,则递归比较 if (is_array($value)) { if (!array_are_equal($value, $array2[$key])) { return false; } } else { if ($value != $array2[$key]) { return false; } } } return true; } }