1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
--TEST--
array_splice() function precerve foreach by reference iterator pointer
--FILE--
<?php
/* remove before */
$done = 0;
$a = [0,1,2,3,4];
foreach($a as &$v) {
echo "$v\n";
if (!$done && $v == 3) {
$done = 1;
array_splice($a, 1, 2);
}
}
echo "\n";
/* remove after */
$done = 0;
$a = [0,1,2,3,4];
foreach($a as &$v) {
echo "$v\n";
if (!$done && $v == 0) {
$done = 1;
array_splice($a, 2, 2);
}
}
echo "\n";
/* remove current */
$done = 0;
$a = [0,1,2,3,4];
foreach($a as &$v) {
echo "$v\n";
if (!$done && $v == 2) {
$done = 1;
array_splice($a, 1, 3);
}
}
echo "\n";
$replacement = ['x', 'y', 'z'];
/* replace before */
$done = 0;
$a = [0,1,2,3,4];
foreach($a as &$v) {
echo "$v\n";
if (!$done && $v == 3) {
$done = 1;
array_splice($a, 1, 2, $replacement);
}
}
echo "\n";
/* replace after */
$done = 0;
$a = [0,1,2,3,4];
foreach($a as &$v) {
echo "$v\n";
if (!$done && $v == 0) {
$done = 1;
array_splice($a, 2, 2, $replacement);
}
}
echo "\n";
/* replace current */
$done = 0;
$a = [0,1,2,3,4];
foreach($a as &$v) {
echo "$v\n";
if (!$done && $v == 2) {
$done = 1;
array_splice($a, 1, 3, $replacement);
}
}
echo "\n";
?>
--EXPECT--
0
1
2
3
4
0
1
4
0
1
2
4
0
1
2
3
4
0
1
x
y
z
4
0
1
2
4
|