xref: /PHP-7.1/ext/standard/tests/array/each.phpt (revision 113213f0)
1--TEST--
2Test each() function
3--FILE--
4<?php
5/* Return the current key and value pair from an array
6   and advance the array cursor */
7
8echo "*** Testing each() : basic functionality ***\n";
9$arrays = array (
10	    array(0),
11	    array(1),
12	    array(-1),
13	    array(1, 2, 3, 4, 5),
14	    array(-1, -2, -3, 6, 7, 8),
15 	    array("a", "ab", "abc", "abcd"),
16	    array("1" => "one", "2" => "two", "3" => "three", "4" => "four"),
17	    array("one" => 1, "two" => 2, 3 => "three", 4 => 4, "" => 5,
18		  "  " => 6, "\x00" => "\x000", "\0" => "\0", "" => "",
19		  TRUE => TRUE, FALSE => FALSE, NULL => NULL
20		 ),
21	    array("1.5" => "one.5", "-2.0" => "negative2.0"),
22	    array(-5 => "negative5", -.05  => "negative.05")
23	  );
24
25/* loop through to check working of each() on different arrays */
26$i = 0;
27while( list( $key, $sub_array) = each($arrays) )  {
28  echo "-- Iteration $i --\n";
29  $c = count ($sub_array);
30  $c++; 		// increment by one to create the situation
31			// of accessing beyond array size
32  while ( $c ) {
33    var_dump( each($sub_array) );
34    $c --;
35  }
36  /* assignment of an array to another variable resets the internal
37     pointer of the array. check this and ensure that each returns
38     the first element after the assignment */
39  $new_array = $sub_array;
40  var_dump( each($sub_array) );
41  echo "\n";
42  $i++;
43}
44
45echo "\n*** Testing each() : possible variations ***\n";
46echo "-- Testing each() with reset() function --\n";
47/* reset the $arrays and use each to get the first element */
48var_dump( reset($arrays) );
49var_dump( each($arrays) );  // first element
50list($key, $sub_array) = each($arrays);  // now second element
51var_dump( each($sub_array) );
52
53
54echo "-- Testing each() with resources --\n";
55$fp = fopen(__FILE__, "r");
56$dfp = opendir(".");
57
58$resources = array (
59  "file" => $fp,
60  "dir" => $dfp
61);
62
63for( $i = 0; $i < count($resources); $i++) {
64  var_dump( each($resources) );
65}
66
67echo "-- Testing each with objects --\n";
68
69class each_class {
70  private $var_private = 100;
71  protected $var_protected = "string";
72  public $var_public = array(0, 1, TRUE, NULL);
73}
74$each_obj = new each_class();
75for( $i = 0; $i <= 2; $i++ ) {
76  var_dump( each($each_obj) );
77}
78
79echo "-- Testing each() with null array --\n";
80$null_array = array();
81var_dump( each($null_array) );
82
83
84echo "\n*** Testing error conditions ***\n";
85
86/* unexpected number of arguments */
87var_dump( each() );  // args = 0
88var_dump( each($null_array, $null_array) );  // args > expected
89
90/* unexpected argument type */
91$var=1;
92$str ="string";
93$fl = "15.5";
94var_dump( each($var) );
95var_dump( each($str) );
96var_dump( each($fl) );
97
98// close resourses used
99fclose($fp);
100closedir($dfp);
101
102echo "Done\n";
103?>
104--EXPECTF--
105*** Testing each() : basic functionality ***
106-- Iteration 0 --
107array(4) {
108  [1]=>
109  int(0)
110  ["value"]=>
111  int(0)
112  [0]=>
113  int(0)
114  ["key"]=>
115  int(0)
116}
117bool(false)
118array(4) {
119  [1]=>
120  int(0)
121  ["value"]=>
122  int(0)
123  [0]=>
124  int(0)
125  ["key"]=>
126  int(0)
127}
128
129-- Iteration 1 --
130array(4) {
131  [1]=>
132  int(1)
133  ["value"]=>
134  int(1)
135  [0]=>
136  int(0)
137  ["key"]=>
138  int(0)
139}
140bool(false)
141array(4) {
142  [1]=>
143  int(1)
144  ["value"]=>
145  int(1)
146  [0]=>
147  int(0)
148  ["key"]=>
149  int(0)
150}
151
152-- Iteration 2 --
153array(4) {
154  [1]=>
155  int(-1)
156  ["value"]=>
157  int(-1)
158  [0]=>
159  int(0)
160  ["key"]=>
161  int(0)
162}
163bool(false)
164array(4) {
165  [1]=>
166  int(-1)
167  ["value"]=>
168  int(-1)
169  [0]=>
170  int(0)
171  ["key"]=>
172  int(0)
173}
174
175-- Iteration 3 --
176array(4) {
177  [1]=>
178  int(1)
179  ["value"]=>
180  int(1)
181  [0]=>
182  int(0)
183  ["key"]=>
184  int(0)
185}
186array(4) {
187  [1]=>
188  int(2)
189  ["value"]=>
190  int(2)
191  [0]=>
192  int(1)
193  ["key"]=>
194  int(1)
195}
196array(4) {
197  [1]=>
198  int(3)
199  ["value"]=>
200  int(3)
201  [0]=>
202  int(2)
203  ["key"]=>
204  int(2)
205}
206array(4) {
207  [1]=>
208  int(4)
209  ["value"]=>
210  int(4)
211  [0]=>
212  int(3)
213  ["key"]=>
214  int(3)
215}
216array(4) {
217  [1]=>
218  int(5)
219  ["value"]=>
220  int(5)
221  [0]=>
222  int(4)
223  ["key"]=>
224  int(4)
225}
226bool(false)
227array(4) {
228  [1]=>
229  int(1)
230  ["value"]=>
231  int(1)
232  [0]=>
233  int(0)
234  ["key"]=>
235  int(0)
236}
237
238-- Iteration 4 --
239array(4) {
240  [1]=>
241  int(-1)
242  ["value"]=>
243  int(-1)
244  [0]=>
245  int(0)
246  ["key"]=>
247  int(0)
248}
249array(4) {
250  [1]=>
251  int(-2)
252  ["value"]=>
253  int(-2)
254  [0]=>
255  int(1)
256  ["key"]=>
257  int(1)
258}
259array(4) {
260  [1]=>
261  int(-3)
262  ["value"]=>
263  int(-3)
264  [0]=>
265  int(2)
266  ["key"]=>
267  int(2)
268}
269array(4) {
270  [1]=>
271  int(6)
272  ["value"]=>
273  int(6)
274  [0]=>
275  int(3)
276  ["key"]=>
277  int(3)
278}
279array(4) {
280  [1]=>
281  int(7)
282  ["value"]=>
283  int(7)
284  [0]=>
285  int(4)
286  ["key"]=>
287  int(4)
288}
289array(4) {
290  [1]=>
291  int(8)
292  ["value"]=>
293  int(8)
294  [0]=>
295  int(5)
296  ["key"]=>
297  int(5)
298}
299bool(false)
300array(4) {
301  [1]=>
302  int(-1)
303  ["value"]=>
304  int(-1)
305  [0]=>
306  int(0)
307  ["key"]=>
308  int(0)
309}
310
311-- Iteration 5 --
312array(4) {
313  [1]=>
314  string(1) "a"
315  ["value"]=>
316  string(1) "a"
317  [0]=>
318  int(0)
319  ["key"]=>
320  int(0)
321}
322array(4) {
323  [1]=>
324  string(2) "ab"
325  ["value"]=>
326  string(2) "ab"
327  [0]=>
328  int(1)
329  ["key"]=>
330  int(1)
331}
332array(4) {
333  [1]=>
334  string(3) "abc"
335  ["value"]=>
336  string(3) "abc"
337  [0]=>
338  int(2)
339  ["key"]=>
340  int(2)
341}
342array(4) {
343  [1]=>
344  string(4) "abcd"
345  ["value"]=>
346  string(4) "abcd"
347  [0]=>
348  int(3)
349  ["key"]=>
350  int(3)
351}
352bool(false)
353array(4) {
354  [1]=>
355  string(1) "a"
356  ["value"]=>
357  string(1) "a"
358  [0]=>
359  int(0)
360  ["key"]=>
361  int(0)
362}
363
364-- Iteration 6 --
365array(4) {
366  [1]=>
367  string(3) "one"
368  ["value"]=>
369  string(3) "one"
370  [0]=>
371  int(1)
372  ["key"]=>
373  int(1)
374}
375array(4) {
376  [1]=>
377  string(3) "two"
378  ["value"]=>
379  string(3) "two"
380  [0]=>
381  int(2)
382  ["key"]=>
383  int(2)
384}
385array(4) {
386  [1]=>
387  string(5) "three"
388  ["value"]=>
389  string(5) "three"
390  [0]=>
391  int(3)
392  ["key"]=>
393  int(3)
394}
395array(4) {
396  [1]=>
397  string(4) "four"
398  ["value"]=>
399  string(4) "four"
400  [0]=>
401  int(4)
402  ["key"]=>
403  int(4)
404}
405bool(false)
406array(4) {
407  [1]=>
408  string(3) "one"
409  ["value"]=>
410  string(3) "one"
411  [0]=>
412  int(1)
413  ["key"]=>
414  int(1)
415}
416
417-- Iteration 7 --
418array(4) {
419  [1]=>
420  int(1)
421  ["value"]=>
422  int(1)
423  [0]=>
424  string(3) "one"
425  ["key"]=>
426  string(3) "one"
427}
428array(4) {
429  [1]=>
430  int(2)
431  ["value"]=>
432  int(2)
433  [0]=>
434  string(3) "two"
435  ["key"]=>
436  string(3) "two"
437}
438array(4) {
439  [1]=>
440  string(5) "three"
441  ["value"]=>
442  string(5) "three"
443  [0]=>
444  int(3)
445  ["key"]=>
446  int(3)
447}
448array(4) {
449  [1]=>
450  int(4)
451  ["value"]=>
452  int(4)
453  [0]=>
454  int(4)
455  ["key"]=>
456  int(4)
457}
458array(4) {
459  [1]=>
460  NULL
461  ["value"]=>
462  NULL
463  [0]=>
464  string(0) ""
465  ["key"]=>
466  string(0) ""
467}
468array(4) {
469  [1]=>
470  int(6)
471  ["value"]=>
472  int(6)
473  [0]=>
474  string(2) "  "
475  ["key"]=>
476  string(2) "  "
477}
478array(4) {
479  [1]=>
480  string(1) "�"
481  ["value"]=>
482  string(1) "�"
483  [0]=>
484  string(1) "�"
485  ["key"]=>
486  string(1) "�"
487}
488array(4) {
489  [1]=>
490  bool(true)
491  ["value"]=>
492  bool(true)
493  [0]=>
494  int(1)
495  ["key"]=>
496  int(1)
497}
498array(4) {
499  [1]=>
500  bool(false)
501  ["value"]=>
502  bool(false)
503  [0]=>
504  int(0)
505  ["key"]=>
506  int(0)
507}
508bool(false)
509array(4) {
510  [1]=>
511  int(1)
512  ["value"]=>
513  int(1)
514  [0]=>
515  string(3) "one"
516  ["key"]=>
517  string(3) "one"
518}
519
520-- Iteration 8 --
521array(4) {
522  [1]=>
523  string(5) "one.5"
524  ["value"]=>
525  string(5) "one.5"
526  [0]=>
527  string(3) "1.5"
528  ["key"]=>
529  string(3) "1.5"
530}
531array(4) {
532  [1]=>
533  string(11) "negative2.0"
534  ["value"]=>
535  string(11) "negative2.0"
536  [0]=>
537  string(4) "-2.0"
538  ["key"]=>
539  string(4) "-2.0"
540}
541bool(false)
542array(4) {
543  [1]=>
544  string(5) "one.5"
545  ["value"]=>
546  string(5) "one.5"
547  [0]=>
548  string(3) "1.5"
549  ["key"]=>
550  string(3) "1.5"
551}
552
553-- Iteration 9 --
554array(4) {
555  [1]=>
556  string(9) "negative5"
557  ["value"]=>
558  string(9) "negative5"
559  [0]=>
560  int(-5)
561  ["key"]=>
562  int(-5)
563}
564array(4) {
565  [1]=>
566  string(11) "negative.05"
567  ["value"]=>
568  string(11) "negative.05"
569  [0]=>
570  int(0)
571  ["key"]=>
572  int(0)
573}
574bool(false)
575array(4) {
576  [1]=>
577  string(9) "negative5"
578  ["value"]=>
579  string(9) "negative5"
580  [0]=>
581  int(-5)
582  ["key"]=>
583  int(-5)
584}
585
586
587*** Testing each() : possible variations ***
588-- Testing each() with reset() function --
589array(1) {
590  [0]=>
591  int(0)
592}
593array(4) {
594  [1]=>
595  array(1) {
596    [0]=>
597    int(0)
598  }
599  ["value"]=>
600  array(1) {
601    [0]=>
602    int(0)
603  }
604  [0]=>
605  int(0)
606  ["key"]=>
607  int(0)
608}
609array(4) {
610  [1]=>
611  int(1)
612  ["value"]=>
613  int(1)
614  [0]=>
615  int(0)
616  ["key"]=>
617  int(0)
618}
619-- Testing each() with resources --
620array(4) {
621  [1]=>
622  resource(%d) of type (stream)
623  ["value"]=>
624  resource(%d) of type (stream)
625  [0]=>
626  string(4) "file"
627  ["key"]=>
628  string(4) "file"
629}
630array(4) {
631  [1]=>
632  resource(%d) of type (stream)
633  ["value"]=>
634  resource(%d) of type (stream)
635  [0]=>
636  string(3) "dir"
637  ["key"]=>
638  string(3) "dir"
639}
640-- Testing each with objects --
641array(4) {
642  [1]=>
643  int(100)
644  ["value"]=>
645  int(100)
646  [0]=>
647  string(23) "�each_class�var_private"
648  ["key"]=>
649  string(23) "�each_class�var_private"
650}
651array(4) {
652  [1]=>
653  string(6) "string"
654  ["value"]=>
655  string(6) "string"
656  [0]=>
657  string(16) "�*�var_protected"
658  ["key"]=>
659  string(16) "�*�var_protected"
660}
661array(4) {
662  [1]=>
663  array(4) {
664    [0]=>
665    int(0)
666    [1]=>
667    int(1)
668    [2]=>
669    bool(true)
670    [3]=>
671    NULL
672  }
673  ["value"]=>
674  array(4) {
675    [0]=>
676    int(0)
677    [1]=>
678    int(1)
679    [2]=>
680    bool(true)
681    [3]=>
682    NULL
683  }
684  [0]=>
685  string(10) "var_public"
686  ["key"]=>
687  string(10) "var_public"
688}
689-- Testing each() with null array --
690bool(false)
691
692*** Testing error conditions ***
693
694Warning: each() expects exactly 1 parameter, 0 given in %s on line %d
695NULL
696
697Warning: each() expects exactly 1 parameter, 2 given in %s on line %d
698NULL
699
700Warning: Variable passed to each() is not an array or object in %s on line %d
701NULL
702
703Warning: Variable passed to each() is not an array or object in %s on line %d
704NULL
705
706Warning: Variable passed to each() is not an array or object in %s on line %d
707NULL
708Done
709