SyntaxStudy
Sign Up
PHP Array Destructuring & Spread
PHP Beginner 1 min read

Array Destructuring & Spread

Array Destructuring & Spread

list() / Short Syntax

Unpack array values into variables in a single statement.

Spread Operator (...)

Unpack an array into a function call or another array. PHP 7.4+ supports spread in arrays.

Example
<?php
// list() / [] destructuring
[$first, $second, $third] = ["apple", "banana", "cherry"];
echo $first;   // apple

// Skip items
[, $second] = [1, 2, 3];
echo $second;  // 2

// Associative destructuring
["name" => $name, "age" => $age] = ["name"=>"Alice","age"=>25];
echo $name; // Alice

// Spread in arrays (PHP 7.4+)
$a = [1, 2, 3];
$b = [0, ...$a, 4, 5];  // [0,1,2,3,4,5]

// Spread in function call
function sum(int ...$nums): int {
    return array_sum($nums);
}
echo sum(...[1,2,3,4,5]); // 15
?>