When I perform a foreach loop over an associatve array in php, the order in which it is performed is the order in which it is defined.
For example:
$arr = array("z" => "z", "a" => "a", "b" => "b");
foreach($arr as $key => val)
print("$key: $val\n");
Outputs:
z: z
a: a
b: b
Whereas
$arr = array("a" => "a", "b" => "b", "z" => "z");
Outputs:
a: a
b: b
z: z
So my question is then: is this behavior defined at a specification level? Can I have reasonable certainty that this behavior will not be changed in future versions of PHP?
From the PHP Manual
and
So you can be certain (at least currently) that the order will be maintained. I would be very surprised if this behaviour were to change because it is clearly stated and because of this there will be a huge amount of code that relies on it.