diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 78d778d8cb31..21f2fd6c4283 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -64,7 +64,7 @@ public static function add($array, $key, $value) /** * Get an array item from an array using "dot" notation. */ - public static function array(ArrayAccess|array $array, string|int|null $key, ?array $default = null): array + public static function array(ArrayAccess|array $array, string|int|null $key, ?array $default = []): array { $value = Arr::get($array, $key, $default); diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index ab5c2f2150a7..0ff52c951867 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -664,6 +664,13 @@ public function testItGetsAnArray() Arr::array($test_array, 'string'); } + public function testItReturnsEmptyArrayForMissingKeyByDefault() + { + $data = ['name' => 'Taylor']; + + $this->assertSame([], Arr::array($data, 'missing_key')); + } + public function testHas() { $array = ['products.desk' => ['price' => 100]]; @@ -1588,7 +1595,8 @@ public function testFrom() $this->assertSame($subject, Arr::from($items)); $items = new WeakMap; - $items[$temp = new class {}] = 'bar'; + $items[$temp = new class { + }] = 'bar'; $this->assertSame(['bar'], Arr::from($items)); $this->expectException(InvalidArgumentException::class);