Bug #70812 [Opn->Nab]: Nested foreach with iterators
Edit report at https://bugs.php.net/bug.php?id=70812&edit=1
ID: 70812
Updated by: bwoebi@php.net
Reported by: fra dot casula at gmail dot com
Summary: Nested foreach with iterators
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Ubuntu 15.10 x64
PHP Version: 5.6.14
Block user comment: N
Private report: N
New Comment:
The difference is that arrays are by-value â they have all own copies which are all
individually iterated. [At least that's how they function semantically.]
Iterators though are objects and thus share a common state.
It's not like arrays would be seeking each time and iterators not. Just each foreach works on
its own array copy, but shares the object.
Previous Comments:
------------------------------------------------------------------------
[2015-10-29 13:49:41] fra dot casula at gmail dot com
Description:
------------
A nested foreach on the same iterator doesn't produce the same result that is produced when
using plain arrays.
The nested foreach should call seek() to reset the internal pointer to its correct position so that
the first foreach can resume where it stopped.
Test script:
---------------
http://pastebin.com/EQrUZ7AQ
Expected result:
----------------
I expect to see the same output for all four cases in the example code. The expected output is:
first loop 0
second loop 0
second loop 1
second loop 2
first loop 1
second loop 0
second loop 1
second loop 2
first loop 2
second loop 0
second loop 1
second loop 2
Actual result:
--------------
first loop 0
second loop 0
second loop 1
second loop 2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70812&edit=1
Thread (5 messages)