Bug #69725 [Opn->Nab]: foreach do not iterate all elements of an array in recursive function
| From: | requinix@php.net | Date: | Fri, 29 May 2015 07:01:10 +0000 |
| Subject: | Bug #69725 [Opn->Nab]: foreach do not iterate all elements of an array in recursive function | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192992@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69725&edit=1
ID: 69725
Updated by: requinix@php.net
Reported by: dp dot maxime at gmail dot com
Summary: foreach do not iterate all elements of an array in
recursive function
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Ubuntu 15.04
PHP Version: 5.6.9
Block user comment: N
Private report: N
New Comment:
References. The explanation for why you get exactly those results is a bit complicated, I'll
explain if you need it, but the short version is that $value is a reference to the last element in
$A so when you set $value=$value+1 in the second foreach you'll also update $A which in turn
also affects $X and $B in the recursive calls.
unset($value) after the first loop to destroy the variable and the reference, then do it after the
second loop too so you learn to do that out of habit.
Previous Comments:
------------------------------------------------------------------------
[2015-05-29 06:36:15] dp dot maxime at gmail dot com
Description:
------------
foreach construct do not iterate all elements of an array in recursive functions.
I am using PHP 5.6.9 from ppa:ondrej/php5-5.6 on Ubuntu 15.04.
The version 5.6.4 from the standard Ubuntu 15.04 distribution is also affected.
Test script:
---------------
<?php
function R(&$X, $level) {
print "$level X: " . json_encode($X) . "\n";
$A = $X;
foreach($A as &$value) {
$value = $value - 1;
}
print "$level A: " . json_encode($A) . "\n";
$B = $X;
foreach($B as &$value) {
$value = $value + 1;
}
print "$level B: " . json_encode($B) . "\n";
if ($level < 3) {
R($B, $level + 1);
}
}
$X = array(1, 2, 3);
R($X, 0);
Expected result:
----------------
0 X: [1,2,3]
0 A: [0,1,2]
0 B: [2,3,4]
1 X: [2,3,4]
1 A: [1,2,3]
1 B: [3,4,5]
2 X: [3,4,5]
2 A: [2,3,4]
2 B: [4,5,6]
3 X: [4,5,6]
3 A: [3,4,5]
3 B: [5,6,7]
Actual result:
--------------
0 X: [1,2,3]
0 A: [0,1,2]
0 B: [2,3,4]
1 X: [2,3,4]
1 A: [1,2,3]
1 B: [3,4,4]
2 X: [3,4,4]
2 A: [2,3,3]
2 B: [4,5,4]
3 X: [4,5,4]
3 A: [3,4,3]
3 B: [5,6,4]
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69725&edit=1