Php array traversal loop implementation program

Source: Internet
Author: User
In php, the most used method for array traversal is foreac, while, and for. let's introduce the program code for these three methods. Someone will often ask me, PHP array

In php, the most used method for array traversal is foreac, while, and for. let's introduce the program code for these three methods.

I am often asked if the PHP array is accessed using foreach, is the traversal order fixed? In what order does it traverse? For example, the following code:

  1. $ Arr ['Yahoo '] = 2007;
  2. $ Arr ['baidu'] = 2008;
  3. Foreach ($ arr as $ key => $ val)
  4. {
  5. // What is the result?
  6. }

For example, the following code:

  1. $ Arr [2] = 'huixinchen ';
  2. $ Arr [2] = 2007; $ arr [0] = 2008;
  3. Foreach ($ arr as $ key => $ val)
  4. {
  5. // What is the current result?
  6. }

When we use the each/next series functions to traverse, we also implement sequential traversal by moving the internal pointer of the array. here is a problem, such as the following code:

  1. $ Arr = array (1, 2, 3, 4, 5 );
  2. Foreach ($ arr as $ v) {// Available}
  3. While (list ($ key, $ v) = each ($ arr ))
  4. {// Cannot be obtained}
  5. ?>

After learning about the knowledge I just introduced, this problem will be very clear, because foreach will automatically reset, while this part will not reset, so after foreach ends, pInternalPointer points to the end of the array, the while statement block cannot be accessed. the solution is to reset the internal pointer of the array before each.

That is to say, the order of traversing arrays in PHP is related to the addition of elements. now we know clearly that the output of the problem at the beginning of the article is:

Huixinchen, 2007,2008

Therefore, if you want to traverse the array of numeric indexes by index size, you should use for instead of foreach. the code is as follows:

  1. For ($ I = 0, $ l = count ($ arr); $ I <$ l; $ I ++)
  2. {// At this time, it cannot be considered sequential traversal (linear traversal )}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.