From:             [EMAIL PROTECTED]
Operating system: Linux (but probably any)
PHP version:      4.2.2
PHP Bug Type:     Scripting Engine problem
Bug description:  nested foreach on same array using reference fails

Try this:
<?php
        $a = array(1, 2);
        $ref =& $a;
        foreach ($ref as $b) {
                echo "outer: $b<br>\n";
                foreach ($a as $c)
                        echo "-- inner: $c<br>\n";
        }
?>

The output is:
outer: 1
-- inner: 1
-- inner: 2
(i.e., the processing stops after the first iteration of the outer
foreach). If I understand the docs well, the output should be:
outer: 1
-- inner: 1
-- inner: 2
outer: 2
-- inner: 1
-- inner: 2
When you remove the ampersand from the assignment to $ref, it works as
expected.

The documentation is a bit unclear on this. It says "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...", which leads me to believe that the sample code should work.
But then it goes on to say: "However, the internal pointer of the original
array *is* advanced with the processing of the array.", which seems to
contradict the first quotation???

This is probably a dupe of bug #14607, but that one is closed as "bogus"
and I can't reopen it. Also see bug #5052, which is similar but not quite,
and it's closed.
-- 
Edit bug report at http://bugs.php.net/?id=21702&edit=1
-- 
Try a CVS snapshot:         http://bugs.php.net/fix.php?id=21702&r=trysnapshot
Fixed in CVS:               http://bugs.php.net/fix.php?id=21702&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=21702&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=21702&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=21702&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=21702&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=21702&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=21702&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=21702&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=21702&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=21702&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=21702&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=21702&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=21702&r=gnused

Reply via email to