Can PHP’s list() work with associative arrays?

With/from PHP 7.1:

For keyed arrays;

$array = ['fruit1' => 'apple', 'fruit2' => 'orange'];

// [] style
['fruit1' => $fruit1, 'fruit2' => $fruit2] = $array;

// list() style
list('fruit1' => $fruit1, 'fruit2' => $fruit2) = $array;

echo $fruit1; // apple

For unkeyed arrays;

$array = ['apple', 'orange'];

// [] style
[$fruit1, $fruit2] = $array;

// list() style
list($fruit1, $fruit2) = $array;

echo $fruit1; // apple

Note: use [] style if possible by version, maybe list goes a new type in the future, who knows…

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)