note 62785 added to control-structures.foreach
| From: | php_man_resp at earthshod dot co dot uk | Date: | Thu, 09 Mar 2006 15:58:04 +0000 |
| Subject: | note 62785 added to control-structures.foreach | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-105148@lists.php.net to get a copy of this message | ||
To atroxodisse: This behaviour is normal. PHP arrays are associative arrays but, unlike Perl, with
artificial order maintenance. Keys can be any scalar value except references; of course integers
are scalars, so traditional integer-indexed arrays just work. {You can even have fractions in array
indices, as long as you enclose the index in speech marks, such as $array["1.5"]! This is
not unique to PHP, by the way, but existed by accident in some dialects of BASIC seen on
1980s-vintage home computers.} The foreach loop iterates over keys in chronological order of when
they were added to the array, which is *not* necessarily the same as the sort order. The print_r()
function uses the same iteration order.
If you want to add elements to the beginning of an existing numeric array {or between existing
elements -- either using fractional indices, or because you left gaps when populating the array}
and have foreach iterate over them in order, then you will need to use ksort() on the array *first*.
----
Server IP: 66.163.161.117
Probable Submitter: 85.189.11.2
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php
Edit -- http://master.php.net/note/edit/62785
Del: integrated -- http://master.php.net/note/delete/62785/integrated
Del: useless -- http://master.php.net/note/delete/62785/useless
Del: bad code -- http://master.php.net/note/delete/62785/bad+code
Del: spam -- http://master.php.net/note/delete/62785/spam
Del: non-english -- http://master.php.net/note/delete/62785/non-english
Del: in docs -- http://master.php.net/note/delete/62785/in+docs
Del: other reasons-- http://master.php.net/note/delete/62785
Reject -- http://master.php.net/note/reject/62785
Search -- http://master.php.net/manage/user-notes.php