#42839 [Fbk->Opn]: Assigning array values inside foreach and array pointer
| From: | denis at edistar dot com | Date: | Mon, 12 Nov 2007 11:10:28 +0000 |
| Subject: | #42839 [Fbk->Opn]: Assigning array values inside foreach and array pointer | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-119529@lists.php.net to get a copy of this message | ||
ID: 42839
User updated by: denis at edistar dot com
Reported By: denis at edistar dot com
-Status: Feedback
+Status: Open
Bug Type: Scripting Engine problem
Operating System: Linux
PHP Version: 5.2.4
New Comment:
The problem still persist. Using the example now the results are:
$parameters key before foreach: a
$parameters key after foreach: b
$parameters key before foreach: b
$parameters key after foreach: b
and not the expected results:
$parameters key before foreach: a
$parameters key after foreach: a
$parameters key before foreach: a
$parameters key after foreach: a
Previous Comments:
------------------------------------------------------------------------
[2007-11-12 10:36:16] tony2001@php.net
Please try using this CVS snapshot:
http://snaps.php.net/php5.2-latest.tar.gz
For Windows (zip):
http://snaps.php.net/win32/php5.2-win32-latest.zip
For Windows (installer):
http://snaps.php.net/win32/php5.2-win32-installer-latest.msi
------------------------------------------------------------------------
[2007-10-03 14:37:29] denis at edistar dot com
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 this bug report at http://bugs.php.net/?id=42839&edit=1