Foreach with reference
If you iterate over an array with & you should unset the value variable after to not modify the array accidentally after.
$array = array('a', 'b', 'c', 'd'); foreach($array as $key=>&$value) { if ($key = 3) { // change d to x $value = 'x'; } } // this will affect the $array to $value = 'z'; // $array = array('a', 'b', 'c', 'z');
Should do like this
$array = array('a', 'b', 'c', 'd'); foreach($array as $key=>&$value) { if ($key = 3) { // change d to x $value = 'x'; } } unset($value); $value = 'z'; // if you do this accidentally you get a notice but no effect on the array // $array = array('a', 'b', 'c', 'x');