This question is always on my mind. I usually alter the array while looping through its keys, based on my gut feelings:
foreach (array_keys($array) as $key) {
$array[$key] = perform_changes_on($array[$key]);
}
I'm aware of other methods, though. The array item can be passed by reference on the foreach call:
foreach ($array as &$item) {
$item = perform_changes_on($item);
}
Finally, the array can be modified directly during the loop:
foreach ($array as $key => $value) {
$array[$key] = perform_changes_on($value);
}
What are the performance and security implications of each approach? Is there a recommended approach?
UPDATE: What I'm actually worried about is that the two last approaches modify the array while I'm looping it.