In-depth understanding of the differences between the for and foreach loop structure traversal arrays in PHP, phpforeach
Traversing an array is the most common method in programming. Here we will discuss the for and foreach methods:
First, we prepare two Arrays for traversal:
$ Arr1 = array (1 => 'A', 3 => 22, 5 => 'B', 4 => 'C', 8 => 'D ');
$ Arr2 = array ('A' => 'aaa', 'B' => 'bbb ', 'c' => 'ccc ', 'D' => 'ddd ', 'E' => 'Eee ');
I. for Loop Structure
Loop 1:
For ($ I = 0, $ num = count ($ arr1); $ I <$ num; $ I ++ ){
Echo $ arr1 [$ I]. '';
}
Output result: a 22 c
Cycle 2:
For ($ I = 0, $ num = count ($ arr2); $ I <$ num; $ I ++ ){
Echo $ arr2 [$ I]. '';
}
This statement is not output
Analysis:
Loop 1 only prints the first two units of array $ arr1, while $ arr2 in loop 2 does not print anything.
The reason is that the for loop is incremented by numbers, so for can only access the index array, for example, in loop 1, the numbers from $ I = 0 to $ I = 4 are incrementally directed to the numbers 0 to 4 in the $ arr1 array, but the keys in the array are: 1, 3, 5, 4, 8. The array units with a key value greater than 4 (5 => 'B', 8 => 'D') will not be accessed because count ($ arr1) = 5, therefore, $ I <5; therefore, the final output result is only a 22 c. For $ arr2, all keys are characters, not numbers, so no output is in loop 2.
Ii. foreach Loop Structure
Cycle 3:
Foreach ($ arr1 as $ key => $ value ){
Echo $ key. '=>'. $ value .'';
}
Output result: 1 => a 3 => 22 5 => B 4 => c 8 => d
Cycle 4:
Foreach ($ arr2 as $ key => $ value ){
Echo $ key. '=>'. $ value .'';
}
Output result: a => aaa B => bbb c => ccc d => ddd e => eee
Analysis:
The foreach loop structure is recycled according to the internal pointer of the array. WhenForeachWhen execution starts, the pointer inside the array automatically points to the first unit. Therefore, the next unit will be obtained in the next loop, and the entire array does not need to be traversed by the array key. This is also the difference between foreach and. Of course,Foreach can only be used for arrays and objectsAndForeachDepending on the internal array pointer, modifying its value in the loop may lead to unexpected behavior.
Note: for operates the value under the corresponding index in each loop, and changes to each value are also reflected in the retrieved object. Foreach, each time a unit is operated, it obtains its index and value into the variable respectively, or only retrieves the value into one variable, and then independently operates the variable with the index and value, does not affect the object to be traversed. If you want to modify the value of an object during the traversal process, add the & symbol before the declaration. For example, foreach ($ array as & $ value ).
Conclusion: if an array uses the key of the most continuous number array unit, you can use the for loop structure. If you are not sure whether the key of the array or the key of the array contains characters, you should use the foreach loop structure.