note 92450 added to control-structures.foreach
| From: | C at osu1 dot php dot net | Date: | Fri, 24 Jul 2009 15:48:59 +0000 |
| Subject: | note 92450 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158695@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 209.41.74.194
Probable Submitter: 78.86.199.170
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- https://master.php.net/note/edit/92450
Del: integrated -- https://master.php.net/note/delete/92450/integrated
Del: useless -- https://master.php.net/note/delete/92450/useless
Del: bad code -- https://master.php.net/note/delete/92450/bad+code
Del: spam -- https://master.php.net/note/delete/92450/spam
Del: non-english -- https://master.php.net/note/delete/92450/non-english
Del: in docs -- https://master.php.net/note/delete/92450/in+docs
Del: other reasons-- https://master.php.net/note/delete/92450
Reject -- https://master.php.net/note/reject/92450
Search -- https://master.php.net/manage/user-notes.php