Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 236355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:20:19+00:00 2026-05-11T20:20:19+00:00

When I perform a foreach loop over an associatve array in php, the order

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:20:19+00:00Added an answer on May 11, 2026 at 8:20 pm

    From the PHP Manual

    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.

    and

    Arrays are ordered. The order can be
    changed using various sorting
    functions.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 158k
  • Answers 158k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer "...XAMPP is not meant for production use but only for… May 12, 2026 at 11:21 am
  • Editorial Team
    Editorial Team added an answer The forum post you mention is a red herring. The… May 12, 2026 at 11:21 am
  • Editorial Team
    Editorial Team added an answer Interfaces describe behavior, and so I name them so as… May 12, 2026 at 11:21 am

Related Questions

I have a List in a web control when the control creates it's child
I have this fragment of code: SmsDataClassesDataContext dc = new SmsDataClassesDataContext(); // Get the
I'm going through the problems on projecteuler.net to learn how to program in Erlang,
I've got the following code: void ReferenceManager_DoWork(object sender, DoWorkEventArgs e) { try { //

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.