Why does casting $arr with (array) cause the array items to not be modified?
$arr = array(1, 2, 3, 4);
foreach ((array)$arr as &$value) {
$value = $value * 2;
}
$arr should now equal [2,4,6,8] but for some reason it still equals [1,2,3,4].
(array), the original array is modified ( repl.it/repls/IllfatedBlueBetatest )(array)cast is a conversion that results in a new array, and modifications to such array are not visible to the original array // Test: repl.it/repls/ShamelessIllfatedFactorial // Conclusion: hypothesis holds.$arr = [1,2,3,4]; $newarr = (array) $arr; $newarr[] = 5; print_r($arr);You wouldn't expect$arrto have the new5entry, because$newarr = (array) $arrdidn't change$arrto an array, it set$newarrto whatever$arrwould be if cast as an array.