Home > Backend Development > PHP Tutorial > What is the difference between for and foreach loop structures?

What is the difference between for and foreach loop structures?

伊谢尔伦
Release: 2023-03-11 07:02:02
Original
2905 people have browsed it

Traversing an array is the most common in programming. Here we will discuss the two methods of for and foreach. There are too many scenarios for using these two methods to perform traversal. Here we only discuss the following two arrays as examples. The so-called "seeing the leopard through the tube" can somewhat clarify the difference between the two.

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');
Copy after login

One: for loopStructure

Loop 1:

<?php
for($i=0,$num=count($arr1);$i<$num;$i++){
    echo $arr1[$i].&#39; &#39;;
}
?>
Copy after login

Output result:

a 22 c
Copy after login

Loop 2:

<?php
for($i=0,$num=count($arr2);$i<$num;$i++){
    echo $arr2[$i].&#39; &#39;;
}
?>
Copy after login

This statement has no output

Analysis:

Loop 1 Only the first two cells of array $arr1 are printed, while $arr2 in loop 2 prints nothing.

The reason is that the for loop is incremented by numbers, so for can only access arrays whose keys are numbers. For example, loop 1 incrementally accesses the keys in the $arr1 array from $i=0 to $i=4. are cells 0 to 4, but the keys in the array are: 1,3,5,4,8. Array units with key values ​​exceeding 4 (5=>'b',8=>'d') will not be accessed, because count($arr1)=5, so $i<5; therefore the final output result Only: a 22 c; for all keys in $arr2 are characters, not numbers, so there is no output in loop 2.

2: foreach loop structure

Loop 3:

<?php
foreach($arr1 as $key=>$value){
    echo $key.&#39;=>&#39;.$value.&#39; &#39;;
}
?>
Copy after login

Output result:

1=>a 3=>22 5=>b 4=>c 8=>d
Copy after login

Loop 4:

<?php
foreach($arr2 as $key=>$value){
    echo $key.&#39;=>&#39;.$value.&#39; &#39;;
}
?>
Copy after login

Output result:

a=>aaa b=>bbb c=>ccc d=>ddd e=>eee
Copy after login

Analysis:

The foreach loop structure loops according to the pointer inside the array. When foreach starts to execute, the pointer inside the array will automatically point to the first unit. Therefore, the next unit will be obtained in the next loop, and there is no need to traverse the entire array according to the array key. This is also the difference between foreach and for. Of course, foreach can only be used with arrays and objects, and since foreach relies on an internal array pointer, modifying its value in a loop will likely lead to unexpected behavior.

Note: Each loop of for operates on the value under the corresponding index, and changes to each value will also be reflected in the traversed object. Each time foreach operates a unit, it takes its index and value into variables respectively, or only takes out the value into one variable, and then operates the variables with the index and value separately, without affecting the traversed object itself. . If you want to modify the value in the object during the traversal process, you need to add an & symbol before the variable in the declaration. For example: foreach($array as &$value).

Conclusion: If an array uses consecutive numbers as the keys of the array units, then you can use the for loop structure. If you are not sure whether the keys of the array or the keys of the array contain characters, you should use the foreach loop structure.

The above is the detailed content of What is the difference between for and foreach loop structures?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template