Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Will PHP sort the array, or it will keep the order of the keys by the order of insertion?

(for example: when doing foreach or when using array_values)

Thank you.

share|improve this question

closed as unclear what you're asking by Amal Murali, tereško, Till Helge, PeeHaa, bensiu Dec 13 '13 at 13:28

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
ksort() use to sort keys php.net/manual/en/array.sorting.php –  user2801382 Dec 13 '13 at 12:53
    
I don't want my keys to be sorted. –  Taru Stolovich Dec 13 '13 at 12:53
    
@TaruStolovich: What exactly are you trying to accomplish? Please explain. –  Amal Murali Dec 13 '13 at 12:55
    
If you just do a foreach it will keep the order you inserted. You should try before ask –  demonofnight Dec 13 '13 at 12:56
    
Of course I tried, but I couldn't know if it has remained that way because it's PHP standard, or just because I've used certain values which have produced hashes that keep them sorted the way I've inserted them accidently. –  Taru Stolovich Dec 13 '13 at 13:00
add comment

1 Answer

up vote 1 down vote accepted

No. To do so, you have to use a sort function.

But if you mean indexed arrays: They are already ordered.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.