Look at the code below
$arr1 = [1, 2]; foreach($arr1 as $key => $value) { $value = $value + 1; } var_dump($key, $value); //结果输出1,3
Description: In the above foreach loop, when the loop is finished, the temporary variable $ key and $ value variable will not be automatically released. Value will be saved. But this time the modification does not affect the value of $ val $ arr.
Foreach understand the principle, the result of the above is easy to understand, when the foreach loop, each loop will copy the value of `$ arr1` element to the temporary variable: $ key, $ value. E.g., on the first iteration, the following
$key = 0; $value = $arr1[0];
When the second cycle, as follows
$key = 1; $value = $arr1[1];
Look forach used references
Quote: If you want to modify elements of the array as they traverse the array, you can use a reference to the foreach $ val in. At this time, $ val referenced elements of the array element points to the current memory address, that is a period of shared memory address. $ Val thus modified while changing the value $ arr [$ key] value.
of arr1 $ = [. 1, 2 ]; the foreach ( $ of arr1 AS $ Key => $ value ) { $ value = $ value +. 1 ; } var_dump ( $ Key , $ value ); // result output l, 3
var_dump ($ of arr1);
$ value of arr1 [ '0' => 2, '1' => 3]
Description: $ key, value and above the $ value of non-quoted case of the same. After using foreach in & references, when the end of the foreach, $ key and $ val variables will not be automatically freed, but at this time and $ val arr $ COUNT ($ arr) - 1 point to the same memory address. Therefore, at this time $ val modified value will also change the $ arr [3] value.
When foreach quote, principles and above, but is a reference copy, as the original
First cycle: $ Key = 0 ; $ value = & $ of arr1 [0 ]; Second cycle: $ Key =. 1 ; $ value = & $ of arr1 [. 1];
Look at an example:
$arr1 = [1, 2]; foreach($arr1 as $key => &$value) { $value = $value + 1; } var_dump($arr1); $value = 100; var_dump($arr1);
When the second output $ arr1, the value of the second element is the 100.
Understand the principles, you know pit. If we have two foreach loop, using the same temporary variable will be a problem, the use of references in a foreach time.
Such as:
of arr1 $ = [. 1, 2 ]; the foreach ( $ of arr1 AS $ Key => & $ value ) { $ value = $ value +. 1 ; }
// At this time $ value = & $ arr1 [1 ], $ value changes back value will change the value of $ arr1 [1] element. arr2 is $ = [10, 20 is, 30 ]; the foreach ( $ arr2 is AS $ value ) { var_dump ( $ of arr1 ); }
We found that when the cycle $ arr2, $ arr2 copied to the $ element value, while the value of the last element of arr1 $ also changes, then the pointer value is equal to $ arr2 is the corresponding element at this time. Sequentially push the last value = $ arr2 elements of the last element $ arr1.
Solution, using unset () release element or the second loop array when using different temporary variables
$arr1 = [1, 2]; foreach($arr1 as $key => &$value) { $value = $value + 1; } unset($value); $arr2 = [10, 20, 30]; foreach ($arr2 as $value) { var_dump($arr1); }