Bug #51951 [Opn->Bgs]: foreach-statement manipulates array (in a special case)

From: Date: Sun, 30 May 2010 18:34:27 +0000
Subject: Bug #51951 [Opn->Bgs]: foreach-statement manipulates array (in a special case)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-150945@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=51951&edit=1

 ID:               51951
 Updated by:       pajoye@php.net
 Reported by:      c336848 at owlpic dot com
 Summary:          foreach-statement manipulates array (in a special
                   case)
-Status:           Open
+Status:           Bogus
 Type:             Bug
 Package:          Scripting Engine problem
 Operating System: Linux
 PHP Version:      Irrelevant

 New Comment:

The last loop modifies the $value from the 1st one, which is a
reference. Simply do:  foreach($my_Array as $val)to see the difference.


Previous Comments:
------------------------------------------------------------------------
[2010-05-30 20:29:00] c336848 at owlpic dot com

Description:
------------
The test script manipulates the last entry of $my_array instead of all
or leaving everything as it is.

my php version: 5.2.10

Test script:
---------------
<?
$my_Array[] = 'a';
$my_Array[] = 'b';
$my_Array[] = 'c';
$my_Array[] = 'd';

echo '<pre>1:' . print_r($my_Array, true) . '</pre>';

foreach($my_Array as &$value){
}

echo '<pre>2:' . print_r($my_Array, true) . '</pre>';

foreach($my_Array as $value){
}

echo '<pre>3:' . print_r($my_Array, true) . '</pre>';
?>

Expected result:
----------------
3:Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)

Actual result:
--------------
3:Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => c
)


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=51951&edit=1


Thread (2 messages)

« previous php.bugs (#150945) next »