DEV Community

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

Posted on

Array Destructuring In PHP

Prerequisite :

To understand this article, you should have a basic understanding of what arrays are and how they work in PHP. If you don't, it's okay, watch the video, I think you would understand, better.

Array destructuring is no new thing in PHP. It's something that has been around but not often used. Prefer a video? I got you.

What exactly is array destructuring?

Array destructuring is simply a way to extract array elements into separate variables.

Let's quickly look at some examples to better understand what this is, shall we?

Good!

Assuming we have this array of brands.

$brands = [ 'Apple', 'Dell', 'Lenovo' ];

Let's pretend that we have received this from a server or something. We might not necessarily want to loop through everything to extract their values. In that case, we might do the following.

Consider this code below:

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

Now, this is a valid PHP code of course, and there's nothing wrong with it, but wouldn't it be nice if we could trim the code a little? Hell yes, we definitely can!

There are two ways to destructure Arrays in PHP.

  1. Using the list() construct
  2. Using the [] shorthand/symmetric syntax

Destructuring Indexed/numerical Arrays

Using The list() Construct

Consider the code below:

list( $apple, $dell, $lenovo ) = $brands;
// we can also do this on one line
$brands = list( $apple, $dell, $lenovo ) = [ 'Apple', 'Dell', 'Lenovo' ];
Enter fullscreen mode Exit fullscreen mode

Skipping an element(s)?

Consider the code below:

// here we are only getting the last element
$brands = list( , , $lenovo ) = [ 'Apple', 'Dell', 'Lenovo' ];
Enter fullscreen mode Exit fullscreen mode

The example above is not very practical because we have an indexed array. So, imagine if we have up to ten, twenty, fifty, or a hundred elements. We are better off with loops and some conditional statements within the loop. The example is merely to illustrate how this works with indexed arrays.

Using The Shorthand Syntax

Now that we have seen how to destructure an indexed array using the list() construct, let's also see how to do the same with the shorthand syntax (square brackets[]).

Consider the following code:

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

Destructuring Associative Arrays

Here we have an array of a person with two elements.

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

Using The list() Construct

Before PHP 7.1.0, the list() construct only worked on indexed arrays. To know the version of PHP installed on your server, run the code below:
echo PHP_VERSION . PHP_EOL;
The PHP_VERSION constant gives you the version number, PHP_EOL is only ending the line for us.

Consider the following code:

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

When working with associative arrays, we don't have to worry about putting the variables in order of the elements. All we need to do is specify the key of the array element we want.

Consider the following code:

// we are getting the age which is the 2nd and last element
// we don't need a comma to skip name which is the first
list( 'age' => $age) = $person;
Enter fullscreen mode Exit fullscreen mode

Using The Shorthand Syntax

Consider the following code:

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

Destructuring Nested Arrays

Consider the following example:

$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

Consider the following example:

$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 the following example:

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

Thanks for reading, I hope it was helpful. 🙂

Top comments (0)