From:             philippe at kinhelios dot com
Operating system: Linux web1106.90.ha.ovh.net 3.2.
PHP version:      Irrelevant
Package:          *General Issues
Bug Type:         Bug
Bug description:Change value in array

Description:
------------
Hi,

Note : PHP Version 5.2.17, but I can't update the version of php, it is
installed on a shared server.

Loop to change the values in an array by reference and then another loop
but same variable name, the initial array is modified again.

[translate by http://www.bing.com/translator/]

Test script:
---------------
<?php
        $arrayOne = array(
                'key1' => array('k1' => 'val_key1_k1'),
                'key2' => array('k1' => 'val_key2_k1')
        );
        
        foreach ($arrayOne as $localKey => &$localValue)        // character & 
                $localValue['k1'] .= '_add';
        
        // -----
        
        print_r($arrayOne);
        
        $counter = 0;
        foreach ($arrayOne as $k => $localValue)                // same name of 
variable
"$localValue", but local
                $counter++;
        
        print_r($arrayOne);                                     // is the same 
array ? => NO !
?>

Expected result:
----------------
                Array
                (
                    [key1] => Array
                        (
                            [k1] => val_key1_k1_add
                        )
                
                    [key2] => Array
                        (
                            [k1] => val_key2_k1_add
                        )
                
                )



Actual result:
--------------
                Array
                (
                    [key1] => Array
                        (
                            [k1] => val_key1_k1_add
                        )
                
                    [key2] => Array
                        (
                            [k1] => val_key1_k1_add     // Hey! My value 
changed here !
                        )
                
                )

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

Reply via email to