diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index e6586edd7f95..277b2d69187a 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -242,7 +242,7 @@ public static function exists($array, $key) } /** - * Return the first element in an array passing a given truth test. + * Return the first element in an iterable passing a given truth test. * * @template TKey * @template TValue @@ -271,6 +271,8 @@ public static function first($array, ?callable $callback = null, $default = null return value($default); } + $array = static::from($array); + $key = array_find_key($array, $callback); return $key !== null ? $array[$key] : value($default); diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index d47d2879614c..ba4019cc9980 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -376,6 +376,15 @@ public function testFirst() $this->assertNull(Arr::first($cursor)); } + public function testFirstWorksWithArrayObject() + { + $arrayObject = new ArrayObject([0, 10, 20]); + + $result = Arr::first($arrayObject, fn ($value) => $value === 0); + + $this->assertSame(0, $result); + } + public function testJoin() { $this->assertSame('a, b, c', Arr::join(['a', 'b', 'c'], ', '));