Summary: In this tutorial, you will learn about the PHP spread operator and how to use it effectively, such as merging multiple arrays into a single one.
Introduction to the PHP spread operator #
PHP 7.4 introduced the spread operator to the array expression. PHP uses the three dots (...
) to denote the spread operator.
When you prefix an array with the spread operator, PHP will spread array elements in place:
...array_var
Code language: PHP (php)
For example:
<?php
$numbers = [4,5];
$scores = [1,2,3, ...$numbers];
print_r($scores);
Code language: PHP (php)
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Code language: PHP (php)
How it works.
- First, define the
$numbers
array that holds two integers. - Second, define the
$scores
array of three integers 1, 2, and 3. Also, spread elements of the$numbers
array into the$scores
array. As a result, the$score
array will contain five numbers 1, 2, 3, 4, and 5.
The spread operator performs better than the array_merge()
function because it is a language construct and a function call. Additionally, PHP optimizes the performance for constant arrays at compile time.
Unlike argument unpacking, you can use the spread operator anywhere. For example, you can use the spread operator at the beginning of the array:
<?php
$numbers = [1,2];
$scores = [...$numbers, 3, 4];
print_r($scores);
Code language: PHP (php)
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Code language: PHP (php)
Or you can use the spread operator in the middle of an array like this:
<?php
$numbers = [2,3];
$scores = [1, ...$numbers, 4];
print_r($scores);
Code language: PHP (php)
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Code language: PHP (php)
Using the spread operator multiple times #
PHP allows you to use the spread operator multiple times. For example:
<?php
$even = [2, 4, 6];
$odd = [1, 2, 3];
$all = [...$odd, ...$even];
print_r($all);
Code language: PHP (php)
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 2
[4] => 4
[5] => 6
)
Code language: PHP (php)
How it works.
- First, define two arrays
$even
and$odd
that hold the even and odd numbers. - Second, use the spread operator to spread elements of these arrays into a new array
$all
. The$all
array will hold the elements of both arrays,$even
and$odd
.
Using spread operator with a return value of a function call #
The following example uses the spread operator with a return value of a function:
<?php
function get_random_numbers()
{
for ($i = 0; $i < 5; $i++) {
$random_numbers[] = rand(1, 100);
}
return $random_numbers;
}
$random_numbers = [...get_random_numbers()];
print_r($random_numbers);
Code language: PHP (php)
Output:
Array
(
[0] => 47
[1] => 78
[2] => 83
[3] => 13
[4] => 32
)
Code language: PHP (php)
How it works.
- First, define a function
get_random_numbers()
that returns an array of five random integers between 1 and 100. - Then, use the spread operator to spread out the elements of the returned array directly from the function call.
Note that you’ll likely see a different output because of the rand()
function.
Using spread operator with a generator #
In the following example, first, we define a generator that returns even numbers between 2 and 10. Then, we use the spread operator to spread out the returned value of the generator into an array:
<?php
function even_number()
{
for($i =2; $i < 10; $i+=2){
yield $i;
}
}
$even = [...even_number()];
print_r($even);
Code language: PHP (php)
Output:
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
)
Code language: PHP (php)
Using spread operator with a Traversable object #
PHP allows you to apply the spread operator not only to an array but also to any object that implements the Traversable
interface. For example:
<?php
class RGB implements IteratorAggregate
{
private $colors = ['red', 'green', 'blue'];
public function getIterator(): Traversable
{
return new ArrayIterator($this->colors);
}
}
$rgb = new RGB();
$colors = [...$rgb];
print_r($colors);
Code language: PHP (php)
Output:
Array
(
[0] => red
[1] => green
[2] => blue
)
Code language: PHP (php)
Spread operator and named arguments #
PHP 8 allows you to call a function using named arguments. For example:
<?php
function format_name(string $first, string $middle, string $last): string
{
return $middle ?
"$first $middle $last" :
"$first $last";
}
echo format_name(
first: 'John',
middle: 'V.',
last: 'Doe'
); // John V. Doe
Code language: PHP (php)
Also, you can pass the arguments to the format_name function using the spread operator:
<?php
function format_name(string $first, string $middle, string $last): string
{
return $middle ?
"$first $middle $last" :
"$first $last";
}
$names = [
'first' => 'John',
'middle' => 'V.',
'last' => 'Doe'
];
echo format_name(...$names); // John V. Doe
Code language: PHP (php)
In this case, the keys of the array elements correspond to the parameter names of the format_name()
function.
Summary #
- PHP uses the three dots (
...
) to denote the spread operator. - The spread operator spreads out the elements of an array (
...array_var
). - Use the spread operator with an array or an object that implements the
Traversable
interface. - Use the PHP spread array to merge two or more arrays into one.