From:             shark555 at gmail dot com
Operating system: Linux
PHP version:      Irrelevant
Package:          *General Issues
Bug Type:         Bug
Bug description:Problem with array behavior after using reference 

Description:
------------
After using same name in first and third foreach script returns wrong
result.
Last element is changed to one just before it.
As we can see it only happens if name of reference and variable used later
in 
foreach is the same("product"). What is worse it affects every next
iteration. 
After that it doesn't matter what is the name of helper variable.

Problem affects PHP versions: 5.2, 5.3 and 5.4 at least.
Different compilations by different people and companies.


Possibly related to: https://bugs.php.net/bug.php?id=60450&edit=2

Test script:
---------------
$products = array(
    array('id'=>1,'name'=>'Cheese'),
    array('id'=>2,'name'=>'Cake'),
    array('id'=>3,'name'=>'Fish'),
    array('id'=>4,'name'=>'Soup'),
    array('id'=>5,'name'=>'Pasta'),
);

$i = 0;
foreach($products as &$product) {
    $product['name'] = $product['name'].$i;
    $i++;
}

foreach($products as $p) {
    echo $p['name'];
    echo "\n";
}

echo "\n";

foreach($products as $product) {
    echo $product['name'];
    echo "\n";
}

echo "\n";

foreach($products as $p) {
    echo $p['name'];
    echo "\n";
}


Expected result:
----------------
Cheese0
Cake1
Fish2
Soup3
Pasta4

Cheese0
Cake1
Fish2
Soup3
Pasta4

Cheese0
Cake1
Fish2
Soup3
Pasta4

Actual result:
--------------
Cheese0
Cake1
Fish2
Soup3
Pasta4

Cheese0
Cake1
Fish2
Soup3
Soup3

Cheese0
Cake1
Fish2
Soup3
Soup3


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

Reply via email to