2 ms·
As far as I can tell from my reading, the strangeness stems from the fact that: > Arrays in PHP are ordered hashtables (i.e. the hash buckets are part of a dou
by Wilduck 14y ago
As far as I can tell from my reading, the strangeness stems from the fact that:
> Arrays in PHP are ordered hashtables (i.e. the hash buckets are part of a doubly linked list)
And that iteration is done using a "internal array pointer":
> This pointer is part of the HashTable structure and is basically just a pointer to the current hashtable Bucket. The internal array pointer is safe against modification, i.e. if the current Bucket is removed, then the internal array pointer will be updated to point to the next bucket.
Which together require some complex copying rules to allow for some simple things like iterating over the same array in nested loops.
I'm not very familiar with the implementation details of many other languages with these constructs, but in python a `for` loop (which operates similarly to the described `foreach` loop in php) simply operates over an iterator, which have a well defined implementation [1]. I don't know about the implementation any deeper than that, however.
I'm curious how other languages implementation of foreach type constructs stack up and how the choice of implementation for the standard list/array datatype affects the interface.
[1] http://excess.org/article/2013/02/itergen1/#iterators http://excess.org/article/2013/02/itergen1/#iterators