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

  • SEARCH
  • Home
  • 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 8997853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:56:42+00:00 2026-06-15T23:56:42+00:00

From what I have found foreach traverses array in the order of adding elements.

  • 0

From what I have found foreach traverses array in the order of adding elements. See this code:

$array = array();
$array[0] = "me0";
$array[1] = "me1";
$array[2] = "me2";
$array[4] = "me4";
$array[5] = "me5";

//changing
$array[3] = "me3Changed";

foreach($array as $item)
{
    echo $item.'<br />';
}

echo '<br />';
ksort($array);

foreach($array as $item)
{
    echo $item.'<br />';
}

which outputs:

me0
me1
me2
me4
me5
me3Changed

me0
me1
me2
me3Changed
me4
me5

This shows that the array is not traversed in a way for($i;$i<$arrayLength;$i++)
how is it traversed than? Assuming that php is written in C++ it should be using some c++ functions that do it this way. Can anyone explain to me how does foreach traverse arrays?


C++ example of traversing array by index:

std::string arr[10]; 
arr[0] = "me0";
arr[1] = "me1";
arr[2] = "me2";
arr[4] = "me4";
arr[5] = "me5";

//changing
arr[3] = "me3Changed";

for(int x = 0; x < 6;x++)
{
    std::cout << arr[x] << std::endl;
}
  • 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-06-15T23:56:43+00:00Added an answer on June 15, 2026 at 11:56 pm

    PHP arrays are ordered key-value stores. Meaning the keys have an order, which is the order in which they were added to the array (or spliced together or sorted or whatever determined the order). foreach traverses arrays in this inherent, built-in order.

    I don’t know if this compares to anything in C. PHP’s arrays are one of its more unique features. Most languages have key-value stores (dictionaries/hashes) or ordered arrays (lists). PHP has ordered key-value stores as its only array type.

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

Sidebar

Related Questions

I have this code which I have found from google. It's a adapter for
I have found many great answers to this from a year ago (when it
I have found the example of using the local sockets . The code from
I have adapted some code from a great article I found about circle drawing
I have found following code to get all action from controller name. Type t
I found this code: AspNetHostingPermissionLevel GetCurrentTrustLevel() { foreach (AspNetHostingPermissionLevel trustLevel in new AspNetHostingPermissionLevel []
I have found this class: https://github.com/lloydwatkin/Demos/blob/master/zendframework/renderifexists/RenderIfExists.php The code looks like this: <?php /** *
I have picked up this code from a tutorial and have edited it make
I have found the result of 'top' is different from result of in standard
I have found Okapi Similarity measure can be used to calculated document similarity from

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.