How to understand the difference between a for and foreach two loop structure traversal array in PHP

Source: Internet
Author: User
Traversing an array is the most common in programming, and here's a discussion of the for and foreach two methods:

First, we'll prepare two arrays for traversal:

$arr 1=array (1=> ' A ', 3=>22, 5=> ' B ', 4=> ' C ', 8=> ' d ');

$arr 2=array (' a ' + = ' aaa ', ' b ' = ' BBB ', ' c ' = ' CCC ', ' d ' = ' ddd ', ' e ' = ' eee ');

One: For loop structure

Cycle 1:

For ($i =0, $num =count ($arr 1), $i < $num, $i + +) {    echo $arr 1[$i]. ' ';}

Output result: A C

Cycle 2:

For ($i =0, $num =count ($arr 2), $i < $num, $i + +) {    echo $arr 2[$i]. ' ';}

There is no output for this segment of the statement

Analysis:

Loop 1 Prints out the first two cells of the array $arr1, and the $arr2 in loop 2 prints nothing.

The reason is that the for loop is incremented by numbers, so for only access to the indexed array, for example, loop 1 increments the $i=0 to $i=4 to access cells with keys 0 through 4 in the $ARR1 array, but the keys in the array are: 1,3,5,4,8. An array unit with a key value greater than 4 (5=> ' B ',8=> ' d ') will not be accessed because count ($arr 1) = 5, so $i<5; So the final output is only: A, C; for all keys in $ARR2 are characters, not numbers, so there is no output in loop 2.

Two: Foreach Loop structure

Cycle 3:

foreach ($arr 1 as $key = + $value) {    echo $key. ' = '. $value. ' ';}

Output result: 1=>a 3=>22 5=>b 4=>c 8=>d

Cycle 4:

foreach ($arr 2 as $key = + $value) {    echo $key. ' = '. $value. ' ';}

Output result: a=>aaa b=>bbb C=>CCC d=>ddd e=>eee

Analysis:

The Foreach loop structure is looped by a pointer inside the array, and when foreach starts executing, the pointer inside the array automatically points to the first cell. So the next loop is going to get the second cell, and you don't need to follow the array's keys to traverse the entire array. This is also the difference between foreach and for. Of course,foreach can only be used with arrays and objects , and because foreach relies on internal array pointers, modifying its values in a loop can cause unexpected behavior.

Note: For Each loop, the values under the corresponding index are manipulated, and the changes to each value are reflected in the object being traversed. Each time foreach operates a cell, it either takes its index and values into a variable, or takes only the value into a variable, and then operates independently of the variable with the index and value, without affecting the traversed object itself. If you want to modify the values in the object during traversal, you need to add the & symbol to the declaration before the variable. For example: foreach ($array as & $value).

Conclusion: If an array is a key that uses contiguous numbers of the most array cells, you can use the For loop structure. If you are unsure whether the keys of an array or the keys of an array contain characters, you should use the Foreach Loop structure:

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.