note 75005 deleted from function.list by googleguy

From: Date: Thu, 13 Sep 2012 10:06:35 +0000
Subject: note 75005 deleted from function.list by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190703@lists.php.net to get a copy of this message
Note Submitter: tobylewis at logogriph dot com ---- The list construct assigns elements from a numbered array starting from element zero. It does not assign elements from associative arrays. So $arr = array(); $arr[1] = 'x'; list($a, $b) = $arr; var_dump($a); //outputs NULL because there is no element [0] var_dump($b); //outputs 'x' and $arr = array('red'=>'stop','green'=>'go'); list($a, $b) = $arr; var_dump($a); //outputs NULL var_dump($b); //outputs NULL If there are not enough elements in the array for the variables in the list the excess variables are assigned NULL. If there are more elements in the array than variables in the list, the extra array elements are ignored without error. Also the warning above about order of assignment is confusing until you get used to php arrays. The order in which array elements are stored is the order in which elements are assigned to the array. So even in a numbered array if you assign $may_arr[2] before you assign $my_array[0] then element [2] will be in the array before [0]. This becomes apparent when using commands like, push, shift or foreach which work with the stored order of the elements. So the warning only applies when the variables in the list are themselves array elements which have not already been assigned to their array.

« previous php.notes (#190703) next »