Specifying Keys in list()

Prior to PHP 7.1, the list() statement for unpacking array elements into variables only supported arrays with numbered keys starting from zero.

PHP 7.1 adds the ability to specify keys when unpacking array elements into variables. In the example shown below, we first construct an array with the keys first, second, and third. We then unpack this array and store the value of $data['first'] in the variable $a, the value of $data['second'] in the variable $b, and the value of $data['third'] in the variable $c:

$data = [
    'first'  => 'foo',
    'second' => 'bar',
    'third'  => 'barbara'

    'first'  => $a,
    'second' => $b,
    'third'  => $c
] = $data;

var_dump($a, $b, $c);

Executing the example shown above with PHP 7.1 will print the output shown below:

string(3) "foo"
string(3) "bar"
string(7) "barbara"

Any expression, including constants and variables, can be used to specify a key.