note 36483 rejected from function.list by vincent
| From: | vincent@php.net | Date: | Sat, 11 Oct 2003 09:28:25 +0000 |
| Subject: | note 36483 rejected from function.list by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58229@lists.php.net to get a copy of this message | ||
Note Submitter: zontar@mindless.com
----
The documentation says that list() only works on numerical arrays, but it would be more accurate to
say that it only works on array elements with numeric indices. At least I found this to be true with
PHP 4.3.2 and 4.3.3 on Win32 and Linux.
For example:
<?php
$people = array(
0 => "bob",
1 => "mary",
"boy" => "jim",
"girl" => "jane",
3 => "ted",
4 => "louise",
"10" => "bill",
"20" => "susie"
);
list($one, $two, $three, $four, $five, $six, $seven, $eight) = $people;
echo "<p>$one</p>";
echo "<p>$two</p>";
echo "<p>$three</p>";
echo "<p>$four</p>";
echo "<p>$five</p>";
echo "<p>$six</p>";
echo "<p>$seven</p>";
echo "<p>$eight</p>";
?>
Output is:
bob
mary
ted
louise
So what list() actually does is to skip over any elements with non-numeric keys.
I'm not sure whether this counts as a bug, an undocumented feature, or something that should be
obvious, but it wasn't apparent to me until I stumbled across it by accident, so perhaps
someone might find it useful.