Arr::mapSpread() Method in Laravel 11.x & Laravel 12.x

In Laravel 11.x & Laravel 12.x, the `Arr::mapSpread()` method is a new addition to the Laravel Collection Helpers that allows you to apply a callback function to an array of arrays, spreading each inner array as arguments to the callback.

Syntax:-

<?php
Arr::mapSpread(callable $callback, array $array): array
?>

$callback → A function that takes multiple arguments (spread from sub-arrays).
$array → A multi-dimensional array where each sub-array is spread into the callback.

Example 1:-

<?php
use Illuminate\Support\Arr;
$array = [
[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9],
];
$mapped = Arr::mapSpread($array, function (int $even, int $odd) {
return $even + $odd;
});
/*
[1, 5, 9, 13, 17]
*/
?>

Example 2: Handling More than Two Arguments

<?php
$data = [
['John', 'Doe', 25],
['Jane', 'Smith', 30]
];

$result = Arr::mapSpread(fn($first, $last, $age) => "$first $last is $age years old", $data);
print_r($result);
?>

Output:-

[
"John Doe is 25 years old",
"Jane Smith is 30 years old"
]

Key Benefits of `Arr::mapSpread()`

1. Simplifies handling multi-dimensional arrays.
2. Works like `map()` but spreads array elements as function arguments.
3. Ideal for transforming structured arrays like key-value pairs.