Is foreach guaranteed to iterate in the array order in php?

Yes ...whatever order you will give to foreach it will iterate in same orders. And in your case array will be sorted in the output file.


The relevant reference might not be that of foreach or sorting as previously mentionned, but that of array instead.

Language reference — Arrays says:

An array in PHP is actually an ordered map.


This is correct, foreach will output in same order as the input array .