Array Destructuring: The Useful PHP 7 Feature You (maybe) Aren't Using

Array Destructuring: The Useful PHP 7 Feature You (maybe) Aren't Using Thumbnail image

What is Array Destructuring

Array Destructuring is a new feature that was introduced with PHP 7.1 (back in December 2016). I haven't really noticed it being used often in any projects or libraries (but maybe I just haven't noticed it). I thought I'd do a quick write up, as I quite like this feature. It helps produce slightly cleaner code.

It kind of replaces the use of list(), and just looks less cluttered in my opinion. I think this is good - it makes scanning and understanding a file much quicker, which in turn leads to faster development.

A basic example - assigning values from an to normal variables

Let's say we have the following array in PHP: an array containing sub-arrays with 3 values - type of vehicle, colour, and brand.

  1.     $vehicles = [
  2.         ['car', 'red', 'Volvo'],
  3.         ['motorbike', 'black', 'Yamaha'],
  4.         ['car', 'red', 'Ford'],
  5.     ];

If you wanted to iterate through each one you could do something like this:

  1. // continued from above ...
  2.     foreach ($vehicles as $vehicle) {
  3.         $type = $vehicle[0];
  4.         $colour = $vehicle[1];
  5.         $brand = $vehicle[2];
  6.         echo "A $colour $type, made by $brand\n";
  7.     }

And to be honest there is absolutely nothing wrong with this code. However, with array destructuring you can make (in my opinion) cleaner code:

  1.     foreach ($vehicles as $vehicle) {
  2.         [$type,$colour,$brand] = $vehicle;
  4.         echo "A $colour $type, made by $brand\n";
  5.     }

This is a very simple syntax that looks like the normal array syntax but in reverse (on the left side).

This will produce the exact same output (so $colour will be equal to 'red' for the first array).

How to ignore the first (or more) items in an array when destructuring an array in PHP

But what you only care about the colour and brand? (i.e. you want to ignore the first item in each array - $vehicle[0])?

There is no need to create an unused variable. You can just do this:

  1.  foreach ($vehicles as $vehicle) {
  3.         [,$colour,$brand] = $vehicle;
  5.         echo "A $colour vehicle, made by $brand\n";
  6.     }

(notice the leading ,)

What is the difference between array destructuring and using list()?

You could also do something like the following, using list() (which has been around since PHP4):

  1.     foreach ($vehicles as $vehicle) {
  2.         list($type,$colour,$brand) = $vehicle;
  3.         echo "A $colour $type, made by $brand\n";
  4.     }

This will produce the exact same output. In these examples there aren't really any differences, except the new way looks nicer and is less cluttered.

There isn't any real difference between list() and this new way of doing it. Like I said - it just looks nicer and cleaner.

A quick note on some changes to list() in PHP 7.1

I know 7.1 came out a couple of years ago, but it is worth mentioning that in PHP 7.1 there were some changes to list(), and you can specify what variables you want to set like this:

  1.     $car = [
  2.         'color' => 'Red',
  3.         'brand' => 'BMW',
  4.     ];
  5.     list('brand' => $company_name) = $car;
  7.     var_dump($company_name); // string(3) "BMW"

The first time I saw this syntax I was sure that it would be an error. Everything about the list() line looks wrong. But it works just fine in 7.1. $company_name is equal to the array's 'brand' item.

Subscribe to my spam free newsletter for other Laravel and Vue updates like this

I never spam, and only email when I have a good in-depth post published on my site (mostly about Laravel and Vue). You can also follow me on social media to get updates.

webdevetc profile pic

I've been working as a software developer for many years (with the last few years specialising in Laravel and Vue). I mostly write about PHP and JavaScript/TypeScript on this site. Contact me here. Need to hire a contract software developer in London, UK (or freelance)? Contact me and check my availability.

Leave a Comment