Here is a simple example of exactly why this must be done: Which do you think is faster? If we just give them references to the original array's values, and they assign some new value to their reference, they would destroy the original array which they aren't allowed to touch!
Lots of people think the answer is two() because it uses "reference to value, which it doesn't have to copy each value when it loops". Here's what actually happens:* one():- This function takes an array as argument ($arr).- The array function argument itself isn't passed by reference, so the function knows it isn't allowed to modify the original at all.- Then the foreach loop happens. ".- So PHP makes a FULL COPY of the ENTIRE array and ALL VALUES before it starts iterating. Therefore: STOP using the old, mythological "&$val" iteration method! With worse performance, and risks of bugs and quirks as is demonstrated in the manual.
This will assign reference instead of copying the value."There are cases where array_walk or array_map are inadequate (conditional required) or you're just too lazy to write a function and pass values to it for use with array_map...The main advantage is that I store only one array, and it's the only array I serialize.An object of My Iter class will not contain any values itself: This will result in:0246810(You may also like to see what var_dump($iterator) produces).:-) But you never do that anyway, when iterating without reference.If you ever want to modify something, you use the "$a[$key] = 123;" method of updating the value. :-) What happened to this note:"Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself.