xref: /PHP-5.4/ext/standard/tests/array/009.phpt (revision 04145dc2)
1--TEST--
2Test key(), current(), next() & reset() functions
3--FILE--
4<?php
5/* Prototype & Usage:
6   mixed key ( array &$array ) -> returns the index element of the current array position
7   mixed current ( array &$array ) -> returns the current element in the array
8   mixed next ( array &$array ) -> similar to current() but advances the internal pointer to next element
9   mixed reset ( array &$array ) -> Reset the internal pointer to first element
10*/
11
12$basic_arrays = array (
13  array(0),  // array with element as 0
14  array(1),  // array with single element
15  array(1,2, 3, -1, -2, -3),               // array of integers
16  array(1.1, 2.2, 3.3, -1.1, -2.2, -3.3),  // array of floats
17  array('a', 'b', 'c', "ab", "ac", "ad"),  // string array
18  array("a" => "apple", "b" => "book", "c" => "cook"),  // associative array
19  array('d' => 'drink', 'p' => 'port', 's' => 'set'),   // another associative array
20  array(1 => 'One', 2 => 'two', 3 => "three")           // associative array with key as integers
21);
22
23$varient_arrays = array (
24   array(),    // empty array
25   array(""),  // array with null string
26   array(NULL),// array with NULL
27   array(null),// array with null
28   array(NULL, true, null, "", 1), // mixed array
29   array(-1.5 => "test", -2 => "rest", 2.5 => "two",
30         "" => "string", 0 => "zero", "" => "" ) // mixed array
31);
32
33echo "*** Testing basic operations ***\n";
34$loop_count = 1;
35foreach ($basic_arrays as $sub_array )  {
36  echo "-- Iteration $loop_count --\n";
37  $loop_count++;
38  $c = count ($sub_array);
39  $c++; // increment by one to create the situation of accessing beyond array size
40  while ( $c ) {
41    var_dump( current($sub_array)); // current element
42    var_dump( key($sub_array) );    // key of the current element
43    var_dump( next($sub_array) );   // move to next element
44    $c --;
45  }
46  var_dump( reset($sub_array) );    // reset the internal pointer to first element
47  var_dump( key($sub_array) );      // access the array after reset
48  var_dump( $sub_array );           // dump the array to see that its intact
49
50  echo "\n";
51}
52
53echo "\n*** Testing possible variations ***\n";
54$loop_count = 1;
55foreach ($varient_arrays as $sub_array )  {
56  echo "-- Iteration $loop_count --\n";
57  $loop_count++;
58  $c = count ($sub_array);
59  $c++; // increment by one to create the situation of accessing beyond array size
60  while ( $c ) {
61    var_dump( current($sub_array)); // current element
62    var_dump( key($sub_array) );    // key of the current element
63    var_dump( next($sub_array) );   // move to next element
64    $c --;
65  }
66  var_dump( reset($sub_array) );    // reset the internal pointer to first element
67  var_dump( key($sub_array) );      // access the array after reset
68  var_dump( $sub_array );           // dump the array to see that its intact
69  echo "\n";
70}
71
72/*test these functions on array which is already unset */
73echo "\n-- Testing variation: when array is unset --\n";
74$unset_array = array (1);
75unset($unset_array);
76
77var_dump( current($unset_array) );
78var_dump( key($unset_array) );
79var_dump( next($unset_array) );
80var_dump( reset($unset_array) );
81
82
83echo "\n*** Testing error conditions ***\n";
84//Zero argument, expected 1 argument
85var_dump( key() );
86var_dump( current() );
87var_dump( reset() );
88var_dump( next() );
89
90// args more than expected, expected 1 argument
91$temp_array = array(1);
92var_dump( key($temp_array, $temp_array) );
93var_dump( current($temp_array, $temp_array) );
94var_dump( reset($temp_array, $temp_array) );
95var_dump( next($temp_array, $temp_array) );
96
97// invalid args type, valid argument: array
98$int_var = 1;
99$float_var = 1.5;
100$string = "string";
101var_dump( key($int_var) );
102var_dump( key($float_var) );
103var_dump( key($string) );
104
105var_dump( current($int_var) );
106var_dump( current($float_var) );
107var_dump( current($string) );
108
109var_dump( next($int_var) );
110var_dump( next($float_var) );
111var_dump( next($string) );
112
113var_dump( reset($int_var) );
114var_dump( reset($float_var) );
115var_dump( reset($string) );
116
117echo "Done\n";
118?>
119--EXPECTF--
120*** Testing basic operations ***
121-- Iteration 1 --
122int(0)
123int(0)
124bool(false)
125bool(false)
126NULL
127bool(false)
128int(0)
129int(0)
130array(1) {
131  [0]=>
132  int(0)
133}
134
135-- Iteration 2 --
136int(1)
137int(0)
138bool(false)
139bool(false)
140NULL
141bool(false)
142int(1)
143int(0)
144array(1) {
145  [0]=>
146  int(1)
147}
148
149-- Iteration 3 --
150int(1)
151int(0)
152int(2)
153int(2)
154int(1)
155int(3)
156int(3)
157int(2)
158int(-1)
159int(-1)
160int(3)
161int(-2)
162int(-2)
163int(4)
164int(-3)
165int(-3)
166int(5)
167bool(false)
168bool(false)
169NULL
170bool(false)
171int(1)
172int(0)
173array(6) {
174  [0]=>
175  int(1)
176  [1]=>
177  int(2)
178  [2]=>
179  int(3)
180  [3]=>
181  int(-1)
182  [4]=>
183  int(-2)
184  [5]=>
185  int(-3)
186}
187
188-- Iteration 4 --
189float(1.1)
190int(0)
191float(2.2)
192float(2.2)
193int(1)
194float(3.3)
195float(3.3)
196int(2)
197float(-1.1)
198float(-1.1)
199int(3)
200float(-2.2)
201float(-2.2)
202int(4)
203float(-3.3)
204float(-3.3)
205int(5)
206bool(false)
207bool(false)
208NULL
209bool(false)
210float(1.1)
211int(0)
212array(6) {
213  [0]=>
214  float(1.1)
215  [1]=>
216  float(2.2)
217  [2]=>
218  float(3.3)
219  [3]=>
220  float(-1.1)
221  [4]=>
222  float(-2.2)
223  [5]=>
224  float(-3.3)
225}
226
227-- Iteration 5 --
228string(1) "a"
229int(0)
230string(1) "b"
231string(1) "b"
232int(1)
233string(1) "c"
234string(1) "c"
235int(2)
236string(2) "ab"
237string(2) "ab"
238int(3)
239string(2) "ac"
240string(2) "ac"
241int(4)
242string(2) "ad"
243string(2) "ad"
244int(5)
245bool(false)
246bool(false)
247NULL
248bool(false)
249string(1) "a"
250int(0)
251array(6) {
252  [0]=>
253  string(1) "a"
254  [1]=>
255  string(1) "b"
256  [2]=>
257  string(1) "c"
258  [3]=>
259  string(2) "ab"
260  [4]=>
261  string(2) "ac"
262  [5]=>
263  string(2) "ad"
264}
265
266-- Iteration 6 --
267string(5) "apple"
268string(1) "a"
269string(4) "book"
270string(4) "book"
271string(1) "b"
272string(4) "cook"
273string(4) "cook"
274string(1) "c"
275bool(false)
276bool(false)
277NULL
278bool(false)
279string(5) "apple"
280string(1) "a"
281array(3) {
282  ["a"]=>
283  string(5) "apple"
284  ["b"]=>
285  string(4) "book"
286  ["c"]=>
287  string(4) "cook"
288}
289
290-- Iteration 7 --
291string(5) "drink"
292string(1) "d"
293string(4) "port"
294string(4) "port"
295string(1) "p"
296string(3) "set"
297string(3) "set"
298string(1) "s"
299bool(false)
300bool(false)
301NULL
302bool(false)
303string(5) "drink"
304string(1) "d"
305array(3) {
306  ["d"]=>
307  string(5) "drink"
308  ["p"]=>
309  string(4) "port"
310  ["s"]=>
311  string(3) "set"
312}
313
314-- Iteration 8 --
315string(3) "One"
316int(1)
317string(3) "two"
318string(3) "two"
319int(2)
320string(5) "three"
321string(5) "three"
322int(3)
323bool(false)
324bool(false)
325NULL
326bool(false)
327string(3) "One"
328int(1)
329array(3) {
330  [1]=>
331  string(3) "One"
332  [2]=>
333  string(3) "two"
334  [3]=>
335  string(5) "three"
336}
337
338
339*** Testing possible variations ***
340-- Iteration 1 --
341bool(false)
342NULL
343bool(false)
344bool(false)
345NULL
346array(0) {
347}
348
349-- Iteration 2 --
350string(0) ""
351int(0)
352bool(false)
353bool(false)
354NULL
355bool(false)
356string(0) ""
357int(0)
358array(1) {
359  [0]=>
360  string(0) ""
361}
362
363-- Iteration 3 --
364NULL
365int(0)
366bool(false)
367bool(false)
368NULL
369bool(false)
370NULL
371int(0)
372array(1) {
373  [0]=>
374  NULL
375}
376
377-- Iteration 4 --
378NULL
379int(0)
380bool(false)
381bool(false)
382NULL
383bool(false)
384NULL
385int(0)
386array(1) {
387  [0]=>
388  NULL
389}
390
391-- Iteration 5 --
392NULL
393int(0)
394bool(true)
395bool(true)
396int(1)
397NULL
398NULL
399int(2)
400string(0) ""
401string(0) ""
402int(3)
403int(1)
404int(1)
405int(4)
406bool(false)
407bool(false)
408NULL
409bool(false)
410NULL
411int(0)
412array(5) {
413  [0]=>
414  NULL
415  [1]=>
416  bool(true)
417  [2]=>
418  NULL
419  [3]=>
420  string(0) ""
421  [4]=>
422  int(1)
423}
424
425-- Iteration 6 --
426string(4) "test"
427int(-1)
428string(4) "rest"
429string(4) "rest"
430int(-2)
431string(3) "two"
432string(3) "two"
433int(2)
434string(0) ""
435string(0) ""
436string(0) ""
437string(4) "zero"
438string(4) "zero"
439int(0)
440bool(false)
441bool(false)
442NULL
443bool(false)
444string(4) "test"
445int(-1)
446array(5) {
447  [-1]=>
448  string(4) "test"
449  [-2]=>
450  string(4) "rest"
451  [2]=>
452  string(3) "two"
453  [""]=>
454  string(0) ""
455  [0]=>
456  string(4) "zero"
457}
458
459
460-- Testing variation: when array is unset --
461
462Warning: current() expects parameter 1 to be array, null given in %s on line %d
463NULL
464
465Warning: key() expects parameter 1 to be array, null given in %s on line %d
466NULL
467
468Warning: next() expects parameter 1 to be array, null given in %s on line %d
469NULL
470
471Warning: reset() expects parameter 1 to be array, null given in %s on line %d
472NULL
473
474*** Testing error conditions ***
475
476Warning: key() expects exactly 1 parameter, 0 given in %s on line %d
477NULL
478
479Warning: current() expects exactly 1 parameter, 0 given in %s on line %d
480NULL
481
482Warning: reset() expects exactly 1 parameter, 0 given in %s on line %d
483NULL
484
485Warning: next() expects exactly 1 parameter, 0 given in %s on line %d
486NULL
487
488Warning: key() expects exactly 1 parameter, 2 given in %s on line %d
489NULL
490
491Warning: current() expects exactly 1 parameter, 2 given in %s on line %d
492NULL
493
494Warning: reset() expects exactly 1 parameter, 2 given in %s on line %d
495NULL
496
497Warning: next() expects exactly 1 parameter, 2 given in %s on line %d
498NULL
499
500Warning: key() expects parameter 1 to be array, integer given in %s on line %d
501NULL
502
503Warning: key() expects parameter 1 to be array, double given in %s on line %d
504NULL
505
506Warning: key() expects parameter 1 to be array, string given in %s on line %d
507NULL
508
509Warning: current() expects parameter 1 to be array, integer given in %s on line %d
510NULL
511
512Warning: current() expects parameter 1 to be array, double given in %s on line %d
513NULL
514
515Warning: current() expects parameter 1 to be array, string given in %s on line %d
516NULL
517
518Warning: next() expects parameter 1 to be array, integer given in %s on line %d
519NULL
520
521Warning: next() expects parameter 1 to be array, double given in %s on line %d
522NULL
523
524Warning: next() expects parameter 1 to be array, string given in %s on line %d
525NULL
526
527Warning: reset() expects parameter 1 to be array, integer given in %s on line %d
528NULL
529
530Warning: reset() expects parameter 1 to be array, double given in %s on line %d
531NULL
532
533Warning: reset() expects parameter 1 to be array, string given in %s on line %d
534NULL
535Done
536