Bug #70471 [NEW]: foreach skips values with array_splice

From: Date: Thu, 10 Sep 2015 19:21:12 +0000
Subject: Bug #70471 [NEW]: foreach skips values with array_splice
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195949@lists.php.net to get a copy of this message
From: dev at cmr dot cx Operating system: Linux PHP version: 7.0.0RC2 Package: Arrays related Bug Type: Bug Bug description:foreach skips values with array_splice Description: ------------ In the expample skript foreach skips the values "c", "d" and "e". As far as I can see this happens only if in the array_splice the number of elements of the replacement array (array("x", "y")) is higher than the length parameter (1). If I set the length to 2, I get the following output, which makes much more sense: 0: a 1: b 3: d 4: e 5: f Array ( [0] => a [1] => x [2] => y [3] => d [4] => e [5] => f ) Test script: --------------- $array = array("a", "b", "c", "d", "e", "f"); foreach ( $array as $key => &$value ) { echo "$key: $value\n"; if ( $value == "b" ) array_splice($array, $key, 1, array("x", "y")); } print_r($array); Expected result: ---------------- 0: a 1: b 3: c 4: d 5: e 6: f Array ( [0] => a [1] => x [2] => y [3] => c [4] => d [5] => e [6] => f ) Actual result: -------------- 0: a 1: b 6: f Array ( [0] => a [1] => x [2] => y [3] => c [4] => d [5] => e [6] => f ) -- Edit bug report at https://bugs.php.net/bug.php?id=70471&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70471&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70471&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70471&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=70471&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=70471&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=70471&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=70471&r=needscript Try newer version: https://bugs.php.net/fix.php?id=70471&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=70471&r=support Expected behavior: https://bugs.php.net/fix.php?id=70471&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=70471&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=70471&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=70471&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70471&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=70471&r=dst IIS Stability: https://bugs.php.net/fix.php?id=70471&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=70471&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=70471&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=70471&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=70471&r=mysqlcfg

« previous php.bugs (#195949) next »