Updating php version
You can even iterate through "dynamic" arrays that do not physically exist, but are objects that implement Iterator interface.
They don't need to be stored in memory when foreach starts.
:-) But you never do that anyway, when iterating without reference.And the value at the current offset (a PHP "zval") is assigned to a variable called $val.- Does $val make a COPY of the value? Telling PHP that you want direct references will force PHP to need to copy the WHOLE array to protect its original values!So instead, just loop normally and trust the fact that PHP *is* actually smart enough to never copy your original array's values!It is recommended to destroy it by unset()."I cannot stress this point of the documentation enough! It's called "copy-on-write" and means that PHP doesn't make any copies unless you try to MODIFY the value.- If you try to MODIFY $val, THEN it will allocate a NEW zval in memory and store $val there instead (but it still won't modify the original array, so you can rest assured). The array itself wasn't passed by reference to the function, so PHP knows that it isn't allowed to modify the outside array.- But it also sees that you want to look at all VALUES by reference (&$val), so PHP says "Uh oh, this is dangerous.
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!('abe','ben','ben') - assigned the value of the third element to itself String keys of associative arrays, for which is_numeric() is true and which can be type-juggled to an int will be cast to an int!