Hi,

there's a problem with foreach when the array it iterates over is a reference or has a reference: contrary to what was clearly written in the documentation for over two years until about two months ago, the foreach doesn't make a copy of the array, and so nested foreach's don't work because the inner foreach moves the array pointer to the end of the array already in the first iteration of the outer array.

About two months ago, the documentation was changed, so that now it isn't obviously incorrect; rather, it is confusing.

I think that the current foreach behaviour is seriously broken. Foreach should never tamper with the internal pointer of the array it iterates over, whether it is a reference or not. Nested foreach's should just work.

I filed bug #21702 on this, but you people are way too quick on the wontfix trigger: one says Bogus without an explanation, after reopening another says Wontfix because of backwards compatibility. He may be right, of course, but I would expect some kind of a discussion. This _is_ an open source project, right?

Obviously, the bugbase is not a good place for discussions, so I'll try it here.

I, personally, find it hard to imagine how someone could knowingly write a script that depends on this broken, undocumented behaviour. Is there some way to find out? Surely there must have been questions like this before. How about releasing a corrected alpha release and seeing if anyone complains?

One more thing: whether this bug is fixed or not, the documentation must be clarified! I don't know about you, but I simply don't understand what it's supposed to say. Quoting: "Note: Also note that foreach operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the each() construct and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array."

Regards,

Vaclav Dvorak <[EMAIL PROTECTED]>


--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to