Is it true that in PHP, when we insert elements into a new hash table using sorted numbers as the keys, then the resulting hash will also be ordered?
So when we get the keys, they will be ordered, and $a[0], $a[1], $a[2] will also follow that original order? (although certainly, the keys will be that order, but the values don’t have to be).
Is it true that in PHP, we can count on that? Is there no such behavior in Perl, Python, or Ruby?
Python has
OrderedDict. Other languages have equivalents as well.However, normally this behavior is not guaranteed for basic hash types (e.g. Python’s
dict) because it requires extra bookkeeping.PHP’s arrays are a special snowflake; it’s best to not get into the habit of relying on basic hashes being ordered, even if you are working with PHP.