This article mainly describes the PHP after the Foreach loop to leave an array of reference problems, has a certain reference value, now share to everyone, the need for friends can refer to
eg
Result
Explain:
1.foreach is not a block-level domain, the end of an array reference, $K, $a not free memory
2. When the first reference loop ends, the $a points to the last element of the array 4
3. When the second start cycle (where $ A is already pointing to the last element, is a reference)
The change of the loop 4-times array is:
1-> [1, 2,3,4]
2-> [1,2, 3,4]
3-> [3,3] The third value points to the last element 3 but value is a global reference in the last loop that points to the last element so value=3 will change the value of the last element to 3.
4-> [1,2,3,3]
Precautions:
The 1.foreach implementation principle has an internal pointer concept
2.& 's foreach Loop end pointer points to the last element
How to avoid:
unset ($a) after circulating with &foreach
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!