Bug #69725 [NEW]: foreach do not iterate all elements of an array in recursive function
| From: | dp dot maxime at gmail dot com | Date: | Fri, 29 May 2015 06:36:15 +0000 |
| Subject: | Bug #69725 [NEW]: foreach do not iterate all elements of an array in recursive function | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-192991@lists.php.net to get a copy of this message | ||
From: dp dot maxime at gmail dot com
Operating system: Ubuntu 15.04
PHP version: 5.6.9
Package: Scripting Engine problem
Bug Type: Bug
Bug description:foreach do not iterate all elements of an array in recursive function
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 bug report at https://bugs.php.net/bug.php?id=69725&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69725&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69725&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69725&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=69725&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=69725&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=69725&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=69725&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=69725&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=69725&r=support
Expected behavior: https://bugs.php.net/fix.php?id=69725&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=69725&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=69725&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=69725&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69725&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=69725&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=69725&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=69725&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69725&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=69725&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=69725&r=mysqlcfg