From:             denis at edistar dot com
Operating system: Linux
PHP version:      5.2.4
PHP Bug Type:     Scripting Engine problem
Bug description:  Assigning array values inside foreach and array pointer

Description:
------------
In PHP 5.2.4 when I set array values inside a foreach, the array pointer
is moved to the second element of the array or, better, to the element
after the first modified element of the array.

In PHP 5.2.3 the array pointer was not moved.

Moreover this does not happen if i assign the array elements outside the
foreach.

Reproduce code:
---------------
$parameters = array(
        'a' => 1,
        'b' => 2,
        'c' => 3,
        'd' => 4
);
echo "\$parameters key before foreach: " . key($parameters) . "\n";
foreach($parameters as $key => $value) {
        $parameters[$key] = "s" . $value;
}
echo "\$parameters key after foreach: " . key($parameters) . "\n";

echo "\$parameters key before foreach: " . key($parameters) . "\n";
$parameters['a'] = "s" . 1;
$parameters['b'] = "s" . 2;
$parameters['c'] = "s" . 3;
$parameters['d'] = "s" . 4;
echo "\$parameters key after foreach: " . key($parameters) . "\n";


Expected result:
----------------
$parameters key before foreach: a
$parameters key before foreach: a

$parameters key before foreach: a
$parameters key before foreach: a

Actual result:
--------------
$parameters key before foreach: a
$parameters key before foreach: b

$parameters key before foreach: a
$parameters key before foreach: a

-- 
Edit bug report at http://bugs.php.net/?id=42839&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=42839&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=42839&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=42839&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=42839&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=42839&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=42839&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=42839&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=42839&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=42839&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=42839&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=42839&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=42839&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=42839&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=42839&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=42839&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=42839&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=42839&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=42839&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=42839&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=42839&r=mysqlcfg

Reply via email to