xref: /PHP-8.0/ext/standard/tests/array/009.phpt (revision b5c7a83d)
1--TEST--
2Test key(), current(), next() & reset() functions
3--FILE--
4<?php
5
6$basic_arrays = array (
7  array(0),  // array with element as 0
8  array(1),  // array with single element
9  array(1,2, 3, -1, -2, -3),               // array of integers
10  array(1.1, 2.2, 3.3, -1.1, -2.2, -3.3),  // array of floats
11  array('a', 'b', 'c', "ab", "ac", "ad"),  // string array
12  array("a" => "apple", "b" => "book", "c" => "cook"),  // associative array
13  array('d' => 'drink', 'p' => 'port', 's' => 'set'),   // another associative array
14  array(1 => 'One', 2 => 'two', 3 => "three")           // associative array with key as integers
15);
16
17$varient_arrays = array (
18   array(),    // empty array
19   array(""),  // array with null string
20   array(NULL),// array with NULL
21   array(null),// array with null
22   array(NULL, true, null, "", 1), // mixed array
23   array(-1.5 => "test", -2 => "rest", 2.5 => "two",
24         "" => "string", 0 => "zero", "" => "" ) // mixed array
25);
26
27echo "*** Testing basic operations ***\n";
28$loop_count = 1;
29foreach ($basic_arrays as $sub_array )  {
30  echo "-- Iteration $loop_count --\n";
31  $loop_count++;
32  $c = count ($sub_array);
33  $c++; // increment by one to create the situation of accessing beyond array size
34  while ( $c ) {
35    var_dump( current($sub_array)); // current element
36    var_dump( key($sub_array) );    // key of the current element
37    var_dump( next($sub_array) );   // move to next element
38    $c --;
39  }
40  var_dump( reset($sub_array) );    // reset the internal pointer to first element
41  var_dump( key($sub_array) );      // access the array after reset
42  var_dump( $sub_array );           // dump the array to see that its intact
43
44  echo "\n";
45}
46
47echo "\n*** Testing possible variations ***\n";
48$loop_count = 1;
49foreach ($varient_arrays as $sub_array )  {
50  echo "-- Iteration $loop_count --\n";
51  $loop_count++;
52  $c = count ($sub_array);
53  $c++; // increment by one to create the situation of accessing beyond array size
54  while ( $c ) {
55    var_dump( current($sub_array)); // current element
56    var_dump( key($sub_array) );    // key of the current element
57    var_dump( next($sub_array) );   // move to next element
58    $c --;
59  }
60  var_dump( reset($sub_array) );    // reset the internal pointer to first element
61  var_dump( key($sub_array) );      // access the array after reset
62  var_dump( $sub_array );           // dump the array to see that its intact
63  echo "\n";
64}
65
66echo "Done\n";
67?>
68--EXPECT--
69*** Testing basic operations ***
70-- Iteration 1 --
71int(0)
72int(0)
73bool(false)
74bool(false)
75NULL
76bool(false)
77int(0)
78int(0)
79array(1) {
80  [0]=>
81  int(0)
82}
83
84-- Iteration 2 --
85int(1)
86int(0)
87bool(false)
88bool(false)
89NULL
90bool(false)
91int(1)
92int(0)
93array(1) {
94  [0]=>
95  int(1)
96}
97
98-- Iteration 3 --
99int(1)
100int(0)
101int(2)
102int(2)
103int(1)
104int(3)
105int(3)
106int(2)
107int(-1)
108int(-1)
109int(3)
110int(-2)
111int(-2)
112int(4)
113int(-3)
114int(-3)
115int(5)
116bool(false)
117bool(false)
118NULL
119bool(false)
120int(1)
121int(0)
122array(6) {
123  [0]=>
124  int(1)
125  [1]=>
126  int(2)
127  [2]=>
128  int(3)
129  [3]=>
130  int(-1)
131  [4]=>
132  int(-2)
133  [5]=>
134  int(-3)
135}
136
137-- Iteration 4 --
138float(1.1)
139int(0)
140float(2.2)
141float(2.2)
142int(1)
143float(3.3)
144float(3.3)
145int(2)
146float(-1.1)
147float(-1.1)
148int(3)
149float(-2.2)
150float(-2.2)
151int(4)
152float(-3.3)
153float(-3.3)
154int(5)
155bool(false)
156bool(false)
157NULL
158bool(false)
159float(1.1)
160int(0)
161array(6) {
162  [0]=>
163  float(1.1)
164  [1]=>
165  float(2.2)
166  [2]=>
167  float(3.3)
168  [3]=>
169  float(-1.1)
170  [4]=>
171  float(-2.2)
172  [5]=>
173  float(-3.3)
174}
175
176-- Iteration 5 --
177string(1) "a"
178int(0)
179string(1) "b"
180string(1) "b"
181int(1)
182string(1) "c"
183string(1) "c"
184int(2)
185string(2) "ab"
186string(2) "ab"
187int(3)
188string(2) "ac"
189string(2) "ac"
190int(4)
191string(2) "ad"
192string(2) "ad"
193int(5)
194bool(false)
195bool(false)
196NULL
197bool(false)
198string(1) "a"
199int(0)
200array(6) {
201  [0]=>
202  string(1) "a"
203  [1]=>
204  string(1) "b"
205  [2]=>
206  string(1) "c"
207  [3]=>
208  string(2) "ab"
209  [4]=>
210  string(2) "ac"
211  [5]=>
212  string(2) "ad"
213}
214
215-- Iteration 6 --
216string(5) "apple"
217string(1) "a"
218string(4) "book"
219string(4) "book"
220string(1) "b"
221string(4) "cook"
222string(4) "cook"
223string(1) "c"
224bool(false)
225bool(false)
226NULL
227bool(false)
228string(5) "apple"
229string(1) "a"
230array(3) {
231  ["a"]=>
232  string(5) "apple"
233  ["b"]=>
234  string(4) "book"
235  ["c"]=>
236  string(4) "cook"
237}
238
239-- Iteration 7 --
240string(5) "drink"
241string(1) "d"
242string(4) "port"
243string(4) "port"
244string(1) "p"
245string(3) "set"
246string(3) "set"
247string(1) "s"
248bool(false)
249bool(false)
250NULL
251bool(false)
252string(5) "drink"
253string(1) "d"
254array(3) {
255  ["d"]=>
256  string(5) "drink"
257  ["p"]=>
258  string(4) "port"
259  ["s"]=>
260  string(3) "set"
261}
262
263-- Iteration 8 --
264string(3) "One"
265int(1)
266string(3) "two"
267string(3) "two"
268int(2)
269string(5) "three"
270string(5) "three"
271int(3)
272bool(false)
273bool(false)
274NULL
275bool(false)
276string(3) "One"
277int(1)
278array(3) {
279  [1]=>
280  string(3) "One"
281  [2]=>
282  string(3) "two"
283  [3]=>
284  string(5) "three"
285}
286
287
288*** Testing possible variations ***
289-- Iteration 1 --
290bool(false)
291NULL
292bool(false)
293bool(false)
294NULL
295array(0) {
296}
297
298-- Iteration 2 --
299string(0) ""
300int(0)
301bool(false)
302bool(false)
303NULL
304bool(false)
305string(0) ""
306int(0)
307array(1) {
308  [0]=>
309  string(0) ""
310}
311
312-- Iteration 3 --
313NULL
314int(0)
315bool(false)
316bool(false)
317NULL
318bool(false)
319NULL
320int(0)
321array(1) {
322  [0]=>
323  NULL
324}
325
326-- Iteration 4 --
327NULL
328int(0)
329bool(false)
330bool(false)
331NULL
332bool(false)
333NULL
334int(0)
335array(1) {
336  [0]=>
337  NULL
338}
339
340-- Iteration 5 --
341NULL
342int(0)
343bool(true)
344bool(true)
345int(1)
346NULL
347NULL
348int(2)
349string(0) ""
350string(0) ""
351int(3)
352int(1)
353int(1)
354int(4)
355bool(false)
356bool(false)
357NULL
358bool(false)
359NULL
360int(0)
361array(5) {
362  [0]=>
363  NULL
364  [1]=>
365  bool(true)
366  [2]=>
367  NULL
368  [3]=>
369  string(0) ""
370  [4]=>
371  int(1)
372}
373
374-- Iteration 6 --
375string(4) "test"
376int(-1)
377string(4) "rest"
378string(4) "rest"
379int(-2)
380string(3) "two"
381string(3) "two"
382int(2)
383string(0) ""
384string(0) ""
385string(0) ""
386string(4) "zero"
387string(4) "zero"
388int(0)
389bool(false)
390bool(false)
391NULL
392bool(false)
393string(4) "test"
394int(-1)
395array(5) {
396  [-1]=>
397  string(4) "test"
398  [-2]=>
399  string(4) "rest"
400  [2]=>
401  string(3) "two"
402  [""]=>
403  string(0) ""
404  [0]=>
405  string(4) "zero"
406}
407
408Done
409