From 9ace91ae59c09992292ae0e7d79232a1ca88546c Mon Sep 17 00:00:00 2001 From: Kay Wei Date: Thu, 28 Dec 2023 10:07:19 +0800 Subject: [PATCH 1/2] Fixes the `Arr::dot()` method to properly handle arrays with integer keys --- src/Illuminate/Collections/Arr.php | 6 +++--- tests/Support/SupportArrTest.php | 7 +++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index d0544154e800..c14465c6b3fa 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -113,13 +113,13 @@ public static function dot($array, $prepend = '') foreach ($array as $key => $value) { if (is_array($value) && ! empty($value)) { - $results[] = static::dot($value, $prepend.$key.'.'); + $results = array_merge($results, static::dot($value, $prepend.$key.'.')); } else { - $results[] = [$prepend.$key => $value]; + $results[$prepend.$key] = $value; } } - return array_merge(...$results); + return $results; } /** diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 3ff9851fab4c..09cec9167431 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -99,8 +99,11 @@ public function testDivide() public function testDot() { - $array = Arr::dot(['foo' => ['bar' => 'baz']]); - $this->assertSame(['foo.bar' => 'baz'], $array); + $array = Arr::dot([10 => 100]); + $this->assertSame([10 => 100], $array); + + $array = Arr::dot(['foo' => [10 => 100]]); + $this->assertSame(['foo.10' => 100], $array); $array = Arr::dot([]); $this->assertSame([], $array); From a3acd73acd920180f2fdd599bd336726ea482f63 Mon Sep 17 00:00:00 2001 From: Kay W Date: Thu, 28 Dec 2023 14:20:23 +0800 Subject: [PATCH 2/2] Update tests/Support/SupportArrTest.php Co-authored-by: Mior Muhammad Zaki --- tests/Support/SupportArrTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index 09cec9167431..083ca5bbb306 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -99,6 +99,9 @@ public function testDivide() public function testDot() { + $array = Arr::dot(['foo' => ['bar' => 'baz']]); + $this->assertSame(['foo.bar' => 'baz'], $array); + $array = Arr::dot([10 => 100]); $this->assertSame([10 => 100], $array);