1 2 3 4 5 6 7 8 9 10 11
<?php $a = ['a1','a2']; var_dump($a); foreach ($a as &$val) {} var_dump($a); $b = ['b3']; foreach ($b as $val) {} var_dump($a); ?>
array(2) { [0]=> string(2) "a1" [1]=> string(2) "a2" } array(2) { [0]=> string(2) "a1" [1]=> &string(2) "a2" } array(2) { [0]=> string(2) "a1" [1]=> &string(2) "b3" }
Solution:
unset($val); // break the reference with the last element