DEV Community

Morcos Gad
Morcos Gad

Posted on

The Splat Operator In PHP And Use It With map() - Laravel

Today we will talk about the Splat Operator, and it gives you the possibility to add any number of parameters inside the function without committing to a certain number, and although it was not used in many cases, but I found it important to share it.
The PHP splat operator (...) has been available in PHP since version 5.6. When it was introduced I made note of it but have never really used it, so I thought it might be interesting to explore it a little.
Take the following function, this takes two parameters and adds them together, returning the output

function addNumbers($number1, $number2) {
  return $number1 + $number2;
}
$numbersArray = [1, 2];
Enter fullscreen mode Exit fullscreen mode

We can call the function using the splat operator like this

echo addNumbers(...$numbersArray);
Enter fullscreen mode Exit fullscreen mode

The following function will take different amounts of numbers and add them together

function addNumbers(...$numbers) {
  $sum = 0;
  foreach ($numbers as $number) {
    $sum += $number;
  }
  return $sum;
}
Enter fullscreen mode Exit fullscreen mode

This is called in the following way

echo addNumbers(1, 2, 3, 4, 5);
Enter fullscreen mode Exit fullscreen mode

since PHP 7.4 it is also possible to run a kind of array merge operation using the splat operator. The following example creates an array and then merges it into the start of a second array

$numbers1 = [1, 2, 3];
$numbers2 = [...$numbers1, 4, 5, 6];
print_r($numbers2);
/*
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
*/
Enter fullscreen mode Exit fullscreen mode

Now let's talk about Collection Map Method
Laravel map() is a Laravel Collections method to pass callback function to each item in Collection. It is a easy yet powerful method to manipulate data in Laravel Collection. After operating on each item, map() method returns new Collection object with updated items

use Illuminate\Support\Collection;
// Create a new collection

$collection = new Collection([
    'jhon', 'tom', 'mike', 'stuart'
]);

// Change all items to uppercase and create a new collection of them
$names = $collection->map(function($item, $key) {
   return strtoupper($item);
});
/*
Collection {#510 ▼
  #items: array:4 [▼
    0 => "JHON"
    1 => "TOM"
    2 => "MIKE"
    3 => "STUART"
  ]
}
*/
Enter fullscreen mode Exit fullscreen mode

Another example

$collection = User::get();
$modified = $collection->map(function($item, $key) {
     return [
        'id' => $item->id,
        'name' => $item->name,
        'creation_date' => $item->created_at->format('m/d/Y')];});
dd($modified);
/*
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Hardik
                    [creation_date] => 04/19/2020
                )
            [1] => Array
                (
                    [id] => 2
                    [name] => HD Surgon
                    [creation_date] => 03/28/2020
                )
        )
)
*/
Enter fullscreen mode Exit fullscreen mode

Finally, you get to use the Splat Operator, meaning map, in an example that shows how to take advantage of these testicles with us

$collection = collect([['id' => 1, 'name' => 'MorCos']]);
$collection->map(fun (array $row) => [...$row, 'tags'=> 'random tag']);
/*
all : [
  [
    "id" => 1,
    "name" => "MorCos",
    "tags" => "random tag"
  ],
]
*/
Enter fullscreen mode Exit fullscreen mode

I hope you enjoyed the code as much as I enjoyed sharing it with you. To dig deeper, check out these sources
https://www.hashbangcode.com/article/splat-operator-php
https://www.larashout.com/laravel-collections-map-method
https://morioh.com/p/06e430499865
https://www.itsolutionstuff.com/post/laravel-collection-map-method-exampleexample.html

Top comments (0)