DEV Community

Cover image for Array Destructuring In PHP
ℵi✗✗
ℵi✗✗

Posted on • Edited on

Array Destructuring In PHP

Prerequisite

To fully understand this article, you should have a basic understanding of what arrays are and how they work in PHP. If you're new to arrays, watching the video below might help you grasp the concept more effectively:


What Is Array Destructuring?

Array destructuring is a way to extract elements from an array into separate variables easily. While it has been around in PHP for a while, it’s not commonly used. Let’s break it down with examples.


Destructuring Indexed/Numerical Arrays

Using the list() Construct

Consider the following array:

$brands = ['Apple', 'Dell', 'Lenovo'];
Enter fullscreen mode Exit fullscreen mode

Without destructuring, you might write:

$apple = $brands[0];
$dell = $brands[1];
$lenovo = $brands[2];
Enter fullscreen mode Exit fullscreen mode

With the list() construct, you can simplify this:

list($apple, $dell, $lenovo) = $brands;

// Alternatively, all on one line:
$brands = list($apple, $dell, $lenovo) = ['Apple', 'Dell', 'Lenovo'];
Enter fullscreen mode Exit fullscreen mode

Skipping Elements

You can skip elements when destructuring:

// Extract only the last element
list(, , $lenovo) = ['Apple', 'Dell', 'Lenovo'];
Enter fullscreen mode Exit fullscreen mode

While this works, it’s more practical to use loops for large arrays with many elements.


Using the Shorthand Syntax (Square Brackets)

Instead of list(), you can use square brackets for destructuring:

$brands = [$apple, $dell, $lenovo] = ['Apple', 'Dell', 'Lenovo'];

// Skipping an element
$brands = [$apple, , $lenovo] = ['Apple', 'Dell', 'Lenovo'];
Enter fullscreen mode Exit fullscreen mode

Destructuring Associative Arrays

Given an associative array:

$person = ['name' => 'Jane Doe', 'age' => 24];
Enter fullscreen mode Exit fullscreen mode

Using the list() Construct

Prior to PHP 7.1.0, list() worked only with indexed arrays. To check your PHP version, use:

echo PHP_VERSION . PHP_EOL;
Enter fullscreen mode Exit fullscreen mode

For associative arrays:

list('name' => $name) = $person;
Enter fullscreen mode Exit fullscreen mode

You can also extract specific keys without considering their order:

list('age' => $age) = $person;
Enter fullscreen mode Exit fullscreen mode

Using the Shorthand Syntax

Here’s how to use square brackets for associative arrays:

['age' => $age] = $person;
Enter fullscreen mode Exit fullscreen mode

Destructuring Nested Arrays

For nested arrays:

$person = [ 
  'name'     => 'Jane Doe', 
  'contacts' => [
    'email' => 'jane@web.com',
    'phone' => '234355663663'
  ]
];

[
  'contacts' => [
    'email' => $email
  ]
] = $person;

// echo $email;
Enter fullscreen mode Exit fullscreen mode

Destructuring in Loops

You can destructure arrays directly within loops:

$people = [ 
  ['id' => '22222', 'name' => 'Jane Doe'],
  ['id' => '33333', 'name' => 'John Doe']
];

foreach ($people as ['id' => $id, 'name' => $name]) {
  // echo $id . '<br />' . $name . '<br /><br />';
}
Enter fullscreen mode Exit fullscreen mode

Destructuring in Practice

Consider a practical example with file uploads:

['name' => $n, 'type' => $t, 'size' => $s] = $_FILES['name_attr_val'];
Enter fullscreen mode Exit fullscreen mode

Array destructuring simplifies code, making it more concise and readable. Whether working with indexed or associative arrays, it’s a valuable tool to include in your PHP toolbox.


Follow Me for More!
Stay connected for more tips, tutorials, and resources:

GitHub
YouTube
LinkedIn

Happy coding!✌️❤️

Top comments (0)