php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #18629 Undocumented behavior of arrays
Submitted: 2002-07-29 14:50 UTC Modified: 2002-07-30 07:02 UTC
From: paul at honeylocust dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.2.2 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: paul at honeylocust dot com
New email:
PHP Version: OS:

 

 [2002-07-29 14:50 UTC] paul at honeylocust dot com
If you iterate an associative array with foreach(),  it seems like it plays elements back in the order in which they were added. This behavior is useful,  but seems to be undocumented.  I'd like to see this documented,  because otherwise I'm afraid this behavior will change in future versions of PHP.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-29 16:55 UTC] jmcastagnetto@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Arrays in PHP are the equivalent to hashes in other languages, so the order of the elements is not assured. The foreach construct will retrieved the data from the hash similarly, so in the general case you should not rely on the order being the original order.

Not sure it should be documented that for PHP compiled on under some OSes and compiler combinations results in the element order being kept. After all the reason for using associative arrays is to be able to access elements by \"name\".
 [2002-07-30 07:02 UTC] rasmus@php.net
Well, actually, PHP arrays are always ordered arrays.  They are hashes in the sense that the index can be anything.  Perhaps a better description is that they are ordered associative arrays.  So yes, you can rely on the order remaining constant and this will not change.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 11 07:01:32 2024 UTC