Let's start with a conclusion, and then we'll use the code to prove the conclusion. $arrtmp = $arr; In this assignment expression, I call $arr an array of assignments, and $arrtmp is called an array of assigned values. If an array pointer to an array of assignments already points to the end of an array when it is assigned a value, the array pointer to the assigned array after the assignment is reset to point to the first element of the array, and if the array pointer to the assignment array does not point to the end of the array when the assignment is made, it points to any valid array element. The array pointer to the assigned array after the assignment is not reset, but rather retains the element it originally pointed to. After the assignment, the assigned array not only has the value of the assignment array, but the array pointer to the assignment array points to that element, and the assigned array points to the same element with the same value in it.
Demo1:
<?php Tutorial
$arr = Array (' var1 ' =>1, ' var2 ' =>2, ' var3 ' =>3, ' VAR4 ' =>4, ' var5 ' =>5);
while (the list ($key, $value) = each ($arr))
{
if ($value = = 4) break;
}
Var_dump (current ($arr));
$arr 1 = $arr;
Var_dump (current ($arr));
Var_dump (current ($arr 1));
?>
The results of the demo1 are: Int (5) int (5) Int (5). From this result, we can see that there is no change in the position of the $arr array pointer before and after the assignment, $arr 1 not only has the same value as $arr, but also the element value that the array pointer points to is the same. Now using the above conclusion to explain this result, in the while loop, there is an if judgment statement, which is designed to keep the $arr array pointer to the end of the array, but to remain in a valid position. The $value=4 jumps out of the loop, and each function moves the array pointer forward one bit, which causes the $arr array pointer to point to the 5th element, so the result of current ($arr) is 5 before the assignment, because before the assignment, the $ The current pointer to the ARR does not point to the end, so the $arr array pointer is not reset after the assignment, but its original position is preserved, so the result of using current ($arr) after the assignment is still 5. When assigned, $ARR1 not only obtains the $arr value, but the array pointer points to the same element as the $arr, both of which are 5.
Demo2:
<?php
$arr = Array (' var1 ' =>1, ' var2 ' =>2, ' var3 ' =>3, ' VAR4 ' =>4, ' var5 ' =>5);
while (the list ($key, $value) = each ($arr))
{
if ($value = = 4) break;
}
Var_dump (current ($arr));
$arr 1 = $arr;
Var_dump (current ($arr));
Var_dump (current ($arr 1));
?>
In Demo2 we break the if ($value = = 4); This comment is dropped, and the purpose is simply to point the $arr array pointer position to the end of the array.
Demo2 Execution Result: bool (FALSE) Int (1) bool (false). If the array pointer corresponds to an element of 0, "", or is not a valid value, the current function returns FALSE, $arr value is not 0 or "", so it can be concluded that the array pointer is pointing to an invalid element and that current returns a false. In other words, it is possible to determine that after the while loop completes, the $arr array pointer has already pointed to the end of the array. So we see that the value of current ($arr) before the assignment is false, and the value of current ($arr) after the assignment becomes 1, indicating that the $arr array pointer was reset after the assignment, pointing to the first element of the array. The value of current ($arr 1) is false, which indicates the element to which the $arr array pointer is $arr1 after the assignment, after which the value is left.
Do not know the description is not clear, or the work of the problem to simplify, use the following code to explain it.
<?php
$arr = Array (' var1 ' =>1, ' var2 ' =>2, ' var3 ' =>3, ' VAR4 ' =>4, ' var5 ' =>5);
while (the list ($key, $value) = each ($arr))
{
$ $key = $value;
}
The next step is the key, the problem is here, after the traversal, add a value to the array.
$arr [' var6 '] = 6;
Func ($arr);
function func ($arrtmp)
{
while (the list ($key, $value) = each ($arrtmp))
{
$ $key = $value;
}
Var_dump ($var 1);
Var_dump ($var 2);
Var_dump ($var 3);
Var_dump ($var 4);
Var_dump ($var 5);
Var_dump ($var 6);
}
?>
Output: null-NULL null-nulls-null int (6).