diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index c217ea8e482c..731fa2949dec 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -2,6 +2,7 @@ namespace Illuminate\Support; +use ArgumentCountError; use ArrayAccess; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; @@ -555,7 +556,11 @@ public static function map(array $array, callable $callback) { $keys = array_keys($array); - $items = array_map($callback, $array, $keys); + try { + $items = array_map($callback, $array, $keys); + } catch (ArgumentCountError) { + $items = array_map($callback, $array); + } return array_combine($keys, $items); } diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index d09fe6cd9a91..a4f5bf0ce6e4 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -632,6 +632,15 @@ public function testMap() $this->assertEquals(['first' => 'taylor', 'last' => 'otwell'], $data); } + public function testMapByReference() + { + $data = ['first' => 'taylor', 'last' => 'otwell']; + $mapped = Arr::map($data, 'strrev'); + + $this->assertEquals(['first' => 'rolyat', 'last' => 'llewto'], $mapped); + $this->assertEquals(['first' => 'taylor', 'last' => 'otwell'], $data); + } + public function testPrepend() { $array = Arr::prepend(['one', 'two', 'three', 'four'], 'zero');