From:             
Operating system: Linux, Windows
PHP version:      5.3.8
Package:          Arrays related
Bug Type:         Bug
Bug description:Looping in a copy of an array using foreach by-reference change 
original array

Description:
------------
On a standard installation of PHP 5.3.x on many OSs (including Linux and
Windows) we:
- set an array with at least two elements
- loop it using foreach by-reference changing each element
- create a copy of the array in another one using the "=" assignment
- loop on the copy as before
- we find the original array changed (usually or always: last element)

Example code given.

Documentation says "Array assignment always involves value copying. Use the
reference operator to copy an array by reference." 

(http://php.net/manual/en/language.types.array.php).

It seems a problem of "foreach" used with the "by-reference" syntax.

Could be related to https://bugs.php.net/bug.php?id=8130 (subject appears
to be the same, but here we have a foreach problem, while nothing is stated
in #8130 about it).

Test script:
---------------
$array1 = array(0 => 'zero', 1 => 'one');
foreach ($array1 as &$elem) { $elem .= ' (1)'; }; // NOTE: if we use
$array1[0] .= ' (1)'; $array1[1] .= ' (1)'; no problem arises
$array2 = $array1;
foreach ($array2 as &$elem) { $elem .= ' (2)'; }; // NOTE: if we use
$array2[0] .= ' (1)'; $array2[1] .= ' (1)'; the problem is still here, so
first foreach seems to be the point
print_r ($array1);
// BUG! EXPECTED: array(0 => 'zero (1)', 1 => 'one (1)'), OUTPUT: array(0
=> 'zero (1)', 1 => 'one (1) (2)')

Expected result:
----------------
Array
(
    [0] => zero (1)
    [1] => one (1)
)

Actual result:
--------------
Array
(
    [0] => zero (1)
    [1] => one (1) (2)
)

-- 
Edit bug report at https://bugs.php.net/bug.php?id=60450&edit=1
-- 
Try a snapshot (PHP 5.4):            
https://bugs.php.net/fix.php?id=60450&r=trysnapshot54
Try a snapshot (PHP 5.3):            
https://bugs.php.net/fix.php?id=60450&r=trysnapshot53
Try a snapshot (trunk):              
https://bugs.php.net/fix.php?id=60450&r=trysnapshottrunk
Fixed in SVN:                        
https://bugs.php.net/fix.php?id=60450&r=fixed
Fixed in SVN and need be documented: 
https://bugs.php.net/fix.php?id=60450&r=needdocs
Fixed in release:                    
https://bugs.php.net/fix.php?id=60450&r=alreadyfixed
Need backtrace:                      
https://bugs.php.net/fix.php?id=60450&r=needtrace
Need Reproduce Script:               
https://bugs.php.net/fix.php?id=60450&r=needscript
Try newer version:                   
https://bugs.php.net/fix.php?id=60450&r=oldversion
Not developer issue:                 
https://bugs.php.net/fix.php?id=60450&r=support
Expected behavior:                   
https://bugs.php.net/fix.php?id=60450&r=notwrong
Not enough info:                     
https://bugs.php.net/fix.php?id=60450&r=notenoughinfo
Submitted twice:                     
https://bugs.php.net/fix.php?id=60450&r=submittedtwice
register_globals:                    
https://bugs.php.net/fix.php?id=60450&r=globals
PHP 4 support discontinued:          
https://bugs.php.net/fix.php?id=60450&r=php4
Daylight Savings:                    https://bugs.php.net/fix.php?id=60450&r=dst
IIS Stability:                       
https://bugs.php.net/fix.php?id=60450&r=isapi
Install GNU Sed:                     
https://bugs.php.net/fix.php?id=60450&r=gnused
Floating point limitations:          
https://bugs.php.net/fix.php?id=60450&r=float
No Zend Extensions:                  
https://bugs.php.net/fix.php?id=60450&r=nozend
MySQL Configuration Error:           
https://bugs.php.net/fix.php?id=60450&r=mysqlcfg

Reply via email to