Wasn't this a given when working with PHP? You can afterall send the reference so that you are modifying the array as you go, rather than at the end.
$array = array(1,2,3,4,5); foreach ($array as &value){...}
$array = array(1,2,3,4,5); foreach ($array as &value){...}
foreach($array as $key => $value)
{
if(someCondition($key))
$array[$key] = someTransformation($value);
}
or whatever - ie. just use the key to modify the original array directly. It's much easier to read and see what's happening (in my opinion anyway). $array = array(1,2,3,4,5);
foreach ($array as &$value) {
echo $value; // some non-mutating action
}
$value = 'woops';
print_r($array); // ([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => woops) foreach ($array as & $ref) {
// Do something with $ref
} unset ($ref);
i.e. put the `unset()` call on the same line as the closing brace, forever "welding" it to that block.