|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-09-10 19:21 UTC] dev at cmr dot cx
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
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 08:00:01 2025 UTC |
The issue is still valid with PHP 7.0.0RC3. However I personally think that the expected result should not skip the key 2 with y value, i.e. it should be 0: a 1: b 2: y 3: c 4: d 5: e 6: f Array ( [0] => a [1] => x [2] => y [3] => c [4] => d [5] => e [6] => f ) This following is a bit simplified test script: <?php $ar = ['a', 'b', 'c']; foreach ($ar as $k => &$v) { echo "$k: $v\n"; if ($k === 0) array_splice($ar, 1, 0, ['x']); } echo "\n"; print_r($ar); Expected result: ---------------- 0: a 1: x 2: b 3: c Array ( [0] => a [1] => x [2] => b [3] => c )