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