rsk82 rsk82 - 1 month ago 6
PHP Question

can list() in latest upcoming php work with associative arrays somehow?

Example:

list($fruit1, $fruit2) = array('apples','oranges');


code above of course works ok, but code below:

list($fruit1, $fruit2) = array('fruit1'=>'apples','fruit2'=>'oranges');


gives:
Notice: Undefined offset: 1 in....


Is there any way to refer to named keys somehow with list like
list('fruit1' : $fruit1)
, have you seen anything like this planned for future release?

Answer

Try the extract function. It will create variables of all your keys, assigned to their associated values:

<?php

extract(array('fruit1'=>'apples','fruit2'=>'oranges'));
var_dump($fruit1);
var_dump($fruit2);