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

 ID:               51951
 Updated by:       paj...@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

Reply via email to