Foreach uses & to traverse the array arr2, and then traverses the array again. The result obtained is very confusing. I wonder if any expert can explain how the & traversal pointer moves.
code show as below:
$arr2 = ['a','s','d'];
foreach ($arr2 as $k => &$v){
echo $k." ".$v."<br>";
}
//unset($v);
foreach ($arr2 as $k => $v){
echo $k." ";
echo $v." ".current($arr2)."<br>";
}
Result:
0 a
1 s
2 d
0 a a
1 s a
2 s a
Why does the pointer stop when it moves to s during the second traversal?
Or you can do this:
Output:
One more thing