note 92450 deleted from control-structures.foreach by danbrown
| From: | danbrown@php.net | Date: | Fri, 24 Jul 2009 15:52:25 +0000 |
| Subject: | note 92450 deleted from control-structures.foreach by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158698@lists.php.net to get a copy of this message | ||
Note Submitter: C
----
Previously was stated the fact that this doesn't behave as expected:
<?php
$foo[3] = "a";
$foo[2] = "b";
$foo[1] = "z";
$foo[0] = "m";
foreach ($foo as $bar) echo $bar . ",";
/* -- output:
a,b,z,m,
*/
?>
The fix was to use "sort()" which sorts alphabetically. Obviously, this is also not
necessarily the intended result (as in my script).
The only solution I found is to simply forget foreach() exists and use a bog-standard
for($i=0;$i<count($foo);$i++) loop. This orders the array by the numerical index you used.