Can PHP's list() work with associative arrays?
With/from PHP 7.1;
$array = ['fruit1' => 'apple', 'fruit2' => 'orange'];
// [] style
['fruit1' => $fruit1, 'fruit2' => $fruit2] = $array;
// list() style
list('fruit1' => $fruit1, 'fruit2' => $fruit2) = $array;
print $fruit1; // apple
EDIT: This approach was useful back in the day (it was asked & answered nine years ago), but see K-Gun's answer below for a better approach with newer PHP 7+ syntax.
Try the extract()
function. It will create variables of all your keys, assigned to their associated values:
extract(array('fruit1' => 'apples', 'fruit2' => 'oranges'));
var_dump($fruit1);
var_dump($fruit2);
What about using array_values()?
<?php
list($fruit1, $fruit2) = array_values( array('fruit1'=>'apples','fruit2'=>'oranges') );
?>