#46123 [Bgs]: Problem using arrays and value references
| From: | martin dot akesson at qbrick dot com | Date: | Fri, 19 Sep 2008 13:39:37 +0000 |
| Subject: | #46123 [Bgs]: Problem using arrays and value references | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-129107@lists.php.net to get a copy of this message | ||
ID: 46123
User updated by: martin dot akesson at qbrick dot com
Reported By: martin dot akesson at qbrick dot com
Status: Bogus
Bug Type: Arrays related
Operating System: FreeBSD 7.0
PHP Version: 5.2.6
New Comment:
Excellent! Thank you for the quick reply and sorry for wasting your
time.
Previous Comments:
------------------------------------------------------------------------
[2008-09-19 13:15:53] scottmac@php.net
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
After the first forach $item is still a reference to the last entry,
you then start to assign it again within your second loop.
The following code gives the behaviour you want.
<?php
$list = array('one', 'two', 'three', 'four');
foreach ($list as &$item) {
$item = "Row $item";
}
unset($item);
print_r($list);
foreach ($list as $index => $item) {
printf("Item #%u: %s\n", $index, $item);
}
var_dump($list);
?>
------------------------------------------------------------------------
[2008-09-19 12:56:28] martin dot akesson at qbrick dot com
Description:
------------
Using a foreach loop to edit values by reference in an array will
mangle
the array. From testing seems the last item in the array will be
replaced by the second last item. It seems the problem shows once the
array has been traversed start to end.
The code to reproduce the problem does a much better job describing the
issue.
Reproduce code:
---------------
<?php
$list = array('one', 'two', 'three', 'four');
foreach ($list as &$item) {
$item = "Row $item";
}
print_r($list);
foreach ($list as $index => $item) {
printf("Item #%u: %s\n", $index, $item);
}
print_r($list);
?>
Expected result:
----------------
Array
(
[0] => Row one
[1] => Row two
[2] => Row three
[3] => Row four
)
Item #0: Row one
Item #1: Row two
Item #2: Row three
Item #3: Row four
Array
(
[0] => Row one
[1] => Row two
[2] => Row three
[3] => Row four
)
Actual result:
--------------
Array
(
[0] => Row one
[1] => Row two
[2] => Row three
[3] => Row four
)
Item #0: Row one
Item #1: Row two
Item #2: Row three
Item #3: Row three
Array
(
[0] => Row one
[1] => Row two
[2] => Row three
[3] => Row three
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46123&edit=1