Let's say I got following code:
$arr = array("1" => 1, "2" => 2, "3" => 3, "4" => "4", "5" => "5");
foreach($arr as $key => $value) {
echo "Array[".$key."]: " . $arr[$key] . "<br>";
echo "Value: " . $value . "<br>";
if (isset($arr[$key+1])) $arr[$key+1] = $arr[$key+1]*2;
}
which creates this output:
Array[1]: 1
Value: 1
Array[2]: 4
Value: 2
Array[3]: 6
Value: 3
Array[4]: 8
Value: 4
Array[5]: 10
Value: 5
Now my question is, why does $value differ from $arr[$key] while iterating through said array? Is $value not getting updated with the array because foreach is just handling a copy of the array or something? Can I solve this issue without using $arr[$key] inside of the loop?