Are PHP Associative Arrays ordered?
I come from python background and the python datatype which is similar (a dictionary) is an unordered set of key value pairs.
I am wondering if PHP associative arrays are unordered? They appear to be ordered.
$test = array( 'test' => 'test', 'bar' => 'bar', ); var_dump($test); var_dump(array_slice($test, 0, 1));
Test always comes before bar and I can slice this array as you see. So is this always guaranteed to be ordered across php versions? Is the order just the order that I have declared the array with? So something is internally pointing 'test' to place  in the array? I have read http://php.net/manual/en/language.types.array.php but it doesn't shed too much light on this issue. I appreciate your responses. Ty
Verified Answer (61 Votes) ✓
Further, PHP allows you to declare arrays with numeric keys out of order:
$a = array(3 => 'three', 1 => 'one', 2 => 'two'); print_r($a); Array (  => three  => one  => two ) // Sort into numeric order ksort($a); print_r($a); Array (  => one  => two  => three )
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
Answer #3 (2 Votes)
From the php manual:
I have relied on the fact that they are ordered and it has worked consistently in every project I've had.