note 75005 added to function.list
| From: | tobylewis at logogriph dot com | Date: | Tue, 08 May 2007 10:55:13 +0000 |
| Subject: | note 75005 added to function.list | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126322@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 194.145.210.3
Probable Submitter: 62.30.219.119
----
Manual Page -- http://www.php.net/manual/en/function.list.php
Edit -- https://master.php.net/note/edit/75005
Del: integrated -- https://master.php.net/note/delete/75005/integrated
Del: useless -- https://master.php.net/note/delete/75005/useless
Del: bad code -- https://master.php.net/note/delete/75005/bad+code
Del: spam -- https://master.php.net/note/delete/75005/spam
Del: non-english -- https://master.php.net/note/delete/75005/non-english
Del: in docs -- https://master.php.net/note/delete/75005/in+docs
Del: other reasons-- https://master.php.net/note/delete/75005
Reject -- https://master.php.net/note/reject/75005
Search -- https://master.php.net/manage/user-notes.php