xref: /PHP-8.4/Zend/tests/foreach_017.phpt (revision ea297654)
1--TEST--
2array_splice() function preserve foreach by reference iterator pointer
3--FILE--
4<?php
5/* remove before */
6$done = 0;
7$a = [0,1,2,3,4];
8foreach($a as &$v) {
9    echo "$v\n";
10    if (!$done && $v == 3) {
11        $done = 1;
12        array_splice($a, 1, 2);
13    }
14}
15echo "\n";
16
17/* remove after */
18$done = 0;
19$a = [0,1,2,3,4];
20foreach($a as &$v) {
21    echo "$v\n";
22    if (!$done && $v == 0) {
23        $done = 1;
24        array_splice($a, 2, 2);
25    }
26}
27echo "\n";
28
29/* remove current */
30$done = 0;
31$a = [0,1,2,3,4];
32foreach($a as &$v) {
33    echo "$v\n";
34    if (!$done && $v == 2) {
35        $done = 1;
36        array_splice($a, 1, 3);
37    }
38}
39echo "\n";
40
41$replacement = ['x', 'y', 'z'];
42
43/* replace before */
44$done = 0;
45$a = [0,1,2,3,4];
46foreach($a as &$v) {
47    echo "$v\n";
48    if (!$done && $v == 3) {
49        $done = 1;
50        array_splice($a, 1, 2, $replacement);
51    }
52}
53echo "\n";
54
55/* replace after */
56$done = 0;
57$a = [0,1,2,3,4];
58foreach($a as &$v) {
59    echo "$v\n";
60    if (!$done && $v == 0) {
61        $done = 1;
62        array_splice($a, 2, 2, $replacement);
63    }
64}
65echo "\n";
66
67/* replace current */
68$done = 0;
69$a = [0,1,2,3,4];
70foreach($a as &$v) {
71    echo "$v\n";
72    if (!$done && $v == 2) {
73        $done = 1;
74        array_splice($a, 1, 3, $replacement);
75    }
76}
77echo "\n";
78?>
79--EXPECT--
800
811
822
833
844
85
860
871
884
89
900
911
922
934
94
950
961
972
983
994
100
1010
1021
103x
104y
105z
1064
107
1080
1091
1102
1114
112