xref: /PHP-8.2/ext/standard/tests/array/005.phpt (revision 3fbd3d2e)
1--TEST--
2Test array_shift() function
3--FILE--
4<?php
5
6$empty_array = array();
7$number = 5;
8$str = "abc";
9
10
11/* Various combinations of arrays to be used for the test */
12$mixed_array = array(
13  array(),
14  array( 1,2,3,4,5,6,7,8,9 ),
15  array( "One", "_Two", "Three", "Four", "Five" ),
16  array( 6, "six", 7, "seven", 8, "eight", 9, "nine" ),
17  array( "a" => "aaa", "A" => "AAA", "c" => "ccc", "d" => "ddd", "e" => "eee" ),
18  array( "1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five" ),
19  array( 1 => "one", 2 => "two", 3 => 7, 4 => "four", 5 => "five" ),
20  array( "f" => "fff", "1" => "one", 4 => 6, "" => "blank", 2 => "float", "F" => "FFF",
21         "blank" => "", 3 => 3.7, 5 => 7, 6 => 8.6, '5' => "Five", "4name" => "jonny", "a" => NULL, NULL => 3 ),
22  array( 12, "name", 'age', '45' ),
23  array( array("oNe", "tWo", 4), array(10, 20, 30, 40, 50), array() ),
24  array( "one" => 1, "one" => 2, "three" => 3, 3, 4, 3 => 33, 4 => 44, 5, 6,
25                              5 => 57, "5.4" => 554, "5.7" => 557 )
26);
27
28/* Testing Error Conditions */
29echo "\n*** Testing Error Conditions ***\n";
30
31/* Empty Array as argument */
32var_dump( array_shift($empty_array) );
33
34/* Loop to test normal functionality with different arrays inputs */
35echo "\n*** Testing with various array inputs ***\n";
36
37$counter = 1;
38foreach( $mixed_array as $sub_array ) {
39  echo "\n-- Input Array for Iteration $counter is --\n";
40  var_dump( $sub_array );
41  echo "\nOutput after shift is :\n";
42  var_dump( array_shift($sub_array) );
43  $counter++;
44}
45
46/*Checking for internal array pointer beint reset when shift is called */
47
48echo"\n*** Checking for internal array pointer being reset when shift is called ***\n";
49
50echo "\nCurrent Element is :";
51var_dump( current($mixed_array[1]) );
52
53echo "\nNext Element is :";
54var_dump( next($mixed_array[1]) );
55
56echo "\nNext Element is :";
57var_dump( next($mixed_array[1]) );
58
59echo "\nshifted Element is :";
60var_dump( array_shift($mixed_array[1]) );
61
62echo "\nCurrent Element after shift operation is:";
63var_dump( current($mixed_array[1]) );
64
65echo"Done";
66?>
67--EXPECT--
68*** Testing Error Conditions ***
69NULL
70
71*** Testing with various array inputs ***
72
73-- Input Array for Iteration 1 is --
74array(0) {
75}
76
77Output after shift is :
78NULL
79
80-- Input Array for Iteration 2 is --
81array(9) {
82  [0]=>
83  int(1)
84  [1]=>
85  int(2)
86  [2]=>
87  int(3)
88  [3]=>
89  int(4)
90  [4]=>
91  int(5)
92  [5]=>
93  int(6)
94  [6]=>
95  int(7)
96  [7]=>
97  int(8)
98  [8]=>
99  int(9)
100}
101
102Output after shift is :
103int(1)
104
105-- Input Array for Iteration 3 is --
106array(5) {
107  [0]=>
108  string(3) "One"
109  [1]=>
110  string(4) "_Two"
111  [2]=>
112  string(5) "Three"
113  [3]=>
114  string(4) "Four"
115  [4]=>
116  string(4) "Five"
117}
118
119Output after shift is :
120string(3) "One"
121
122-- Input Array for Iteration 4 is --
123array(8) {
124  [0]=>
125  int(6)
126  [1]=>
127  string(3) "six"
128  [2]=>
129  int(7)
130  [3]=>
131  string(5) "seven"
132  [4]=>
133  int(8)
134  [5]=>
135  string(5) "eight"
136  [6]=>
137  int(9)
138  [7]=>
139  string(4) "nine"
140}
141
142Output after shift is :
143int(6)
144
145-- Input Array for Iteration 5 is --
146array(5) {
147  ["a"]=>
148  string(3) "aaa"
149  ["A"]=>
150  string(3) "AAA"
151  ["c"]=>
152  string(3) "ccc"
153  ["d"]=>
154  string(3) "ddd"
155  ["e"]=>
156  string(3) "eee"
157}
158
159Output after shift is :
160string(3) "aaa"
161
162-- Input Array for Iteration 6 is --
163array(5) {
164  [1]=>
165  string(3) "one"
166  [2]=>
167  string(3) "two"
168  [3]=>
169  string(5) "three"
170  [4]=>
171  string(4) "four"
172  [5]=>
173  string(4) "five"
174}
175
176Output after shift is :
177string(3) "one"
178
179-- Input Array for Iteration 7 is --
180array(5) {
181  [1]=>
182  string(3) "one"
183  [2]=>
184  string(3) "two"
185  [3]=>
186  int(7)
187  [4]=>
188  string(4) "four"
189  [5]=>
190  string(4) "five"
191}
192
193Output after shift is :
194string(3) "one"
195
196-- Input Array for Iteration 8 is --
197array(12) {
198  ["f"]=>
199  string(3) "fff"
200  [1]=>
201  string(3) "one"
202  [4]=>
203  int(6)
204  [""]=>
205  int(3)
206  [2]=>
207  string(5) "float"
208  ["F"]=>
209  string(3) "FFF"
210  ["blank"]=>
211  string(0) ""
212  [3]=>
213  float(3.7)
214  [5]=>
215  string(4) "Five"
216  [6]=>
217  float(8.6)
218  ["4name"]=>
219  string(5) "jonny"
220  ["a"]=>
221  NULL
222}
223
224Output after shift is :
225string(3) "fff"
226
227-- Input Array for Iteration 9 is --
228array(4) {
229  [0]=>
230  int(12)
231  [1]=>
232  string(4) "name"
233  [2]=>
234  string(3) "age"
235  [3]=>
236  string(2) "45"
237}
238
239Output after shift is :
240int(12)
241
242-- Input Array for Iteration 10 is --
243array(3) {
244  [0]=>
245  array(3) {
246    [0]=>
247    string(3) "oNe"
248    [1]=>
249    string(3) "tWo"
250    [2]=>
251    int(4)
252  }
253  [1]=>
254  array(5) {
255    [0]=>
256    int(10)
257    [1]=>
258    int(20)
259    [2]=>
260    int(30)
261    [3]=>
262    int(40)
263    [4]=>
264    int(50)
265  }
266  [2]=>
267  array(0) {
268  }
269}
270
271Output after shift is :
272array(3) {
273  [0]=>
274  string(3) "oNe"
275  [1]=>
276  string(3) "tWo"
277  [2]=>
278  int(4)
279}
280
281-- Input Array for Iteration 11 is --
282array(10) {
283  ["one"]=>
284  int(2)
285  ["three"]=>
286  int(3)
287  [0]=>
288  int(3)
289  [1]=>
290  int(4)
291  [3]=>
292  int(33)
293  [4]=>
294  int(44)
295  [5]=>
296  int(57)
297  [6]=>
298  int(6)
299  ["5.4"]=>
300  int(554)
301  ["5.7"]=>
302  int(557)
303}
304
305Output after shift is :
306int(2)
307
308*** Checking for internal array pointer being reset when shift is called ***
309
310Current Element is :int(1)
311
312Next Element is :int(2)
313
314Next Element is :int(3)
315
316shifted Element is :int(1)
317
318Current Element after shift operation is:int(2)
319Done
320