This article describes the two uses of the PHP foreach as $key and $value, has a certain reference value, now share to everyone, the need for friends can refer to
Introduction to a foreach syntax
PHP 4 and above includes a foreach structure, which is simply a simple way to iterate through an array. foreach can only be used with arrays, and an error occurs when trying to use it for other data types or an uninitialized variable. There are two kinds of syntax, and the second is the first kind of useful extension.
1 2 |
foreach (array_expression as $value) Statementforeach (array_expression as $key = $value) statement
|
The first format iterates through the given array of array_expression. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (so the next cell in the next loop will be taken).
The second format does the same thing, except that the key value is assigned to the variable $key in each loop in addition to the current cell value. Look at the following code:
1 2 3 4 5 6 7 8 9 |
$arr = Array ("1" = "111", "2" = "222", "3" = "333"), and foreach ($arr as $key = = $value) {echo $key. = ". $value." \ n ";} The results are as follows: 1=>1112=>2223=>333
|
The key value can be understood here as an array subscript, and the subscript for the element a[2] is 2.
When foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call Reset () before the Foreach loop. While loop requires reset. The following two types of code function exactly the same.
1. Using the While loop
1 2 3 4 5 |
$arr = Array ("One", "one", "one", "three"), Reset ($arr), while (list (, $value) = each ($arr)) {echo "Value: $value <br>\n";}
|
2. Using the Foreach
1 2 3 |
foreach ($arr as $value) {echo "Value: $value <br>\n";}
|
Also note that foreach is manipulating a copy of the specified array, not the array itself. So even with each () construct, the original array pointer does not change, and the value of the array cell is unaffected.
foreach does not support the ability to suppress error messages with "@".
Two PHP foreach Summary
The case of using references in 1.foreach.
In general, $arr and $value in foreach ($arr as $value) are copies that are not externally affected, i.e.
1 2 3 4 5 6 7 8 9 |
$arr = Array (0,1,2,3,4,5), foreach ($arr as $value) {$arr = array (); Echo $value;}
|
The result is: 12345
But if $arr is quoted, the situation is different, and we use the code to illustrate the problem
1 2 3 4 5 6 7 8 9 10 11 |
$arr = Array (0,1,2,3,4,5), $arr = & $arr, foreach ($arr as $value) {$arr = array (); Echo $value;}
|
The result is: 0
This is because the $arr of the loop is directed directly to the original data, not a copy.
If $value is a reference, and $arr is not a reference, the result is the same, and the same $value is pointing to the original data instead of the copy
1 2 3 4 5 6 7 8 9 |
$arr = Array (0,1,2,3,4,5), foreach ($arr as & $value) {$arr = array (); Echo $value;}
|
The result is: 0
There is also a special case, that is, if the $arr is defined as a global variable, $arr will also become a reference
1 2 3 4 5 6 7 8 9 10 11 |
Global $arr; $arr = Array (0,1,2,3,4,5), foreach ($arr as $value) {$arr = array (); Echo $value;}
|
The result is: 0
2. If two loops an array, it must not be written like this
1 2 3 |
foreach ($arr as & $value) {}foreach ($arr as $value) {}
|
This causes the result of the second loop to be incorrect (possibly a PHP bug). Can be replaced by the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st |
Solution 1foreach ($arr as & $value) {}unset ($value), foreach ($arr as $value) {}//solution 2foreach ($arr as &$ Value) {}foreach ($arr as & $value) {}//solution 3foreach ($arr as & $value) {} $arr 2 = $arr; foreach ($arr 2 as $value) {}
|
Related recommendations:
A detailed description of the usage and examples of foreach in PHP