PHP Array Destructuring

Created with Sketch.

PHP Array Destructuring

Summary: in this tutorial, you’ll learn how to use PHP array destructuring to assign elements of an array to multiple variables.

Introduction to the PHP array destructuring

Suppose that you have an array returned by a function parse_url():

<?php

$urls = parse_url('https://www.python-tutorials.in/');
var_dump($urls);

Code language: PHP (php)

Output:

array(3) {
["scheme"]=> string(5) "https"
["host"]=> string(19) "www.python-tutorials.in"
["path"]=> string(1) "/"
}

Code language: PHP (php)

To assign the elements of the array to multiple variables, you can use the list() construct like this:

<?php

list('scheme' => $scheme,
'host' => $host,
'path'=>$path
) = parse_url('https://www.python-tutorials.in/');

var_dump($scheme, $host, $path);

Code language: PHP (php)

Output:

string(5) "https"
string(19) "www.python-tutorials.in"
string(1) "/"

Code language: PHP (php)

PHP 7.1 introduced a new way of unpacking the array’s elements to variables. It’s called array destructuring:

<?php

[
'scheme' => $scheme,
'host' => $host,
'path'=>$path
] = parse_url('https://www.python-tutorials.in/');

var_dump($scheme, $host, $path);

Code language: PHP (php)

Output:

string(5) "https"
string(19) "www.python-tutorials.in"
string(1) "/"

Code language: PHP (php)

Like the list(), the array destructuring works with both indexed and associative arrays. For example:

<?php

$person = ['John','Doe'];
[$first_name, $last_name] = $person;

var_dump($first_name, $last_name);

Code language: PHP (php)

Output:

string(4) "John"
string(3) "Doe"

Code language: PHP (php)

Skipping elements

The array destructuring syntax also allows you to skip any elements. For example, the following skips the second element of the array:

<?php

$person = ['John','Doe', 24];
[$first_name, , $age] = $person;

var_dump($first_name, $age);

Code language: PHP (php)

Output:

string(4) "John"
int(24)

Code language: PHP (php)

PHP array destructuring examples

Let’s take some examples of using the PHP array destructuring syntax.

1) Swaping variables

To swap the values of two variables, you often use a temporary variable like this:

<?php

$x = 10;
$y = 20;

// swap variables
$tmp = $x;
$x = $y;
$y = $tmp;

var_dump($x, $y);

Code language: PHP (php)

Now, you can use the array destructuring syntax to make the code shorter:

<?php

$x = 10;
$y = 20;

// swap variables
[$x, $y] = [$y, $x];

var_dump($x, $y);

Code language: PHP (php)

2) Parsing an array returned from a function

Many functions in PHP return an array. The array destructuring allows you to parse an array returned from a function.

For example:

<?php

[
'dirname' => $dirname,
'basename' => $basename
] = pathinfo('c:\temp\readme.txt');

var_dump($dirname, $basename);

Code language: PHP (php)

Output:

string(7) "c:\temp"
string(10) "readme.txt"

Code language: PHP (php)

Summary

  • Use the PHP array destructuring to unpack elements of an array to variables.

Leave a Reply

Your email address will not be published. Required fields are marked *