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;
  3.  
  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) {
  2.  
  3.         [,$colour,$brand] = $vehicle;
  4.  
  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;
  6.  
  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.

webdevetc profile pic
webdevetc

I am a 29 year old freelance backend web developer from London, mostly focusing on PHP and Laravel lately. This is my site - I mostly write about PHP here. Contact me here (especially for any contracting jobs early 2019 in London ;) ).

Leave a Comment