Mastering PHP Spread Operator (...
) for Arrays
The PHP spread operator (...
) — introduced in PHP 7.4 for arrays — is a powerful feature that often flies under the radar. It's especially handy when working with complex array structures or combining multiple arrays into one.
In this post, we’ll demystify the spread operator in PHP, explore practical use cases, and highlight how you can use it to write cleaner and more expressive code.
🔹 What is the Spread Operator in PHP?
The spread operator (...
) allows you to unpack arrays into argument lists or array literals.
Example:
$letters1 = ['a', 'b'];
$letters2 = ['c', 'd'];
$all = [...$letters1, ...$letters2]; // ['a', 'b', 'c', 'd']
💡 Use Case 1: Flatten Arrays for array_merge()
Let’s say you have an array of arrays and want to merge them all:
$data = [
['a', 'b'],
['s', 't', 'u'],
['x', 'y'],
['c'],
['d', 'e']
];
$merged = array_merge(...$data);
print_r($merged);
// Output: ['a', 'b', 's', 't', 'u', 'x', 'y', 'c', 'd', 'e']
✅ The ...
operator unpacks each inner array and feeds them as individual arguments to array_merge()
.
⚠️ Mind-Bending Example: Double Spread
This works too:
$merged = array_merge(...[...[['a', 'b'], ['s', 't', 'u'], ['x', 'y']], ['c'], ['d', 'e']]);
Here’s what’s happening:
- The inner spread (
...[[...], [..]]
) flattens nested arrays into individual array arguments. - The outer spread passes them to
array_merge()
.
💥 Output is still:
['a', 'b', 's', 't', 'u', 'x', 'y', 'c', 'd', 'e']
Cool, right?
🔹 Use Case 2: Multiple Parameters in Functions
function greet($name, $age) {
return "Hello, $name! You are $age.";
}
$params = ['Alamin', 28];
echo greet(...$params); // Hello, Alamin! You are 28.
This is especially useful when you’re dynamically collecting parameters and passing them to a function.
🔹 Use Case 3: Associative Arrays in PHP 8+
function createUser($name, $email) {
return "$name <$email>";
}
$data = ['name' => 'Alamin', 'email' => '[email protected]'];
echo createUser(...$data); // PHP 8+
❌ When the Spread Operator Doesn’t Work
You can’t use ...
with functions expecting a single array argument — like:
array_map()
array_filter()
count()
sort()
Example that fails:
array_map(...[['strtoupper'], ['a', 'b']]); // ❌ Error
✅ Rule of Thumb
Function Type | Spread Supported? |
---|---|
Accepts multiple arguments | ✅ Yes |
Accepts only one array parameter | ❌ No |
🛠 Bonus: Helper to Deep Flatten Any Array
function flatten(array $array): array {
$result = [];
array_walk_recursive($array, function ($item) use (&$result) {
$result[] = $item;
});
return $result;
}
$nested = [['a', 'b'], ['c', ['d', 'e']], 'f'];
print_r(flatten($nested));
// Output: ['a', 'b', 'c', 'd', 'e', 'f']
🧠 Final Thoughts
The spread operator in PHP is more than syntactic sugar — it brings expressive power to your array operations, simplifies argument handling, and makes your code easier to read.
Next time you’re merging arrays or working with dynamic arguments, consider using ...
for a cleaner, modern PHP approach.
Top comments (0)