OneCompiler

Foreach Reference Trap

111
<?php $array = [1,2,3,4,5]; foreach($array as &$value){ $value *= 2; } <!-- So this reference will change the main array to [2,4,6,8,10] --> <!-- But before of the below logic or code we need to unset the $value otherwise if I add $value = 100 then last element value will be change 10 to 100. --> unset($value); <!-- try to substruct from each element from updated array --> $result = array_map(fn($element) => $element - 1, $array); echo implode(', ', $result);