In my project I increase values of firstArray +3.After I increase values of secondArray +7 .
$firstArray = array(
'a' => 1,
'b' => 3,
'c' => 5,
);
foreach ($firstArray as &$item) {
$item += 3;
}
print_r($firstArray);
Result: Array ( [a] => 4 [b] => 6 [c] => 8)
$secondArray = array(
'a' => 11,
'b' => 13,
'c' => 17,
);
foreach ($secondArray as $key=>$item) {
$secondArray[$key] += 7;
}
print_r($secondArray);
Result: Array ( [a] => 18 [b] => 20 [c] => 24 )
But problem is when I did print_r($firstArray) again; I have this result:
Array ( [a] => 4 [b] => 6 [c] => 17 )
Why there is the difference in result fisrt and thisrd?
Result: Array ( [a] => 12 [b] => 22 [c] => 32 )from?&) in the first loop, you need tounset($item)immediately after that loop otherwise it's still a reference to the last entry in$firstArray... loopingby referenceshould always be approached with caution