php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46123 Problem using arrays and value references
Submitted: 2008-09-19 12:56 UTC Modified: 2008-09-19 13:39 UTC
From: martin dot akesson at qbrick dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.6 OS: FreeBSD 7.0
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: martin dot akesson at qbrick dot com
New email:
PHP Version: OS:

 

 [2008-09-19 12:56 UTC] 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
)





Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-19 13:15 UTC] 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 13:39 UTC] martin dot akesson at qbrick dot com
Excellent!  Thank you for the quick reply and sorry for wasting your time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 15:01:33 2024 UTC