From d9d8b5607707d089b494efee0958f54dbb503626 Mon Sep 17 00:00:00 2001 From: DavidLiu Date: Wed, 23 Oct 2024 21:23:32 +0800 Subject: [PATCH] fix bug in merge method of class Arr $array1 = ['a' => 'x']; $array2 = ['a' => [1, 2]]; Arr::merge($array1, $array2); above code wiil throw exception: Argument 1 passed to Hyperf\Collection\Arr::merge() must be of the type array, string given, called in ... --- src/Arr.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Arr.php b/src/Arr.php index 464ed71..f2e1591 100644 --- a/src/Arr.php +++ b/src/Arr.php @@ -569,11 +569,11 @@ public static function merge(array $array1, array $array2, bool $unique = true): { $isAssoc = static::isAssoc($array1 ?: $array2); if ($isAssoc) { - foreach ($array2 as $key => $value) { - if (is_array($value)) { - $array1[$key] = static::merge($array1[$key] ?? [], $value, $unique); + foreach ($array2 as $key => $value2) { + if (is_array($value2) && isset($array1[$key]) && is_array($array1[$key])) { + $array1[$key] = static::merge($array1[$key], $value2, $unique); } else { - $array1[$key] = $value; + $array1[$key] = $value2; } } } else {