Use the foreach loop array as much as possible because it is more efficient than for, but sometimes it may be a little risky. let's take a look at the warning in the PHP manual: the above description is clear. let's look at an example: & amp; lt ;? Php // byhttp
Use the foreach loop array as much as possible because it is more efficient than for, but sometimes it may be a little risky. let's take a look at the warning in the PHP manual:
The above description is clear. let's look at an example:
\ N ";}echo $ value; // threeforeach ($ arr as $ value) {echo" Value: $ value
\ N ";}
The printed result is as follows:
Value: oneValue: twoValue: threethreeValue: oneValue: twoValue: two
Why didn't we expect the array to be printed for the second time? Obviously, the reference operation is the array itself. after the first loop ends, $ value still points to the last element of array $, in fact, the second loop only assigns values to three times of $ arr [2.
The correct method is to destroy the pointer $ value (unset) after the first loop.