xref: /PHP-7.0/ext/standard/tests/array/each.phpt (revision 330168a2)
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
105--EXPECTF--
106*** Testing each() : basic functionality ***
107-- Iteration 0 --
108array(4) {
109  [1]=>
110  int(0)
111  ["value"]=>
112  int(0)
113  [0]=>
114  int(0)
115  ["key"]=>
116  int(0)
117}
118bool(false)
119array(4) {
120  [1]=>
121  int(0)
122  ["value"]=>
123  int(0)
124  [0]=>
125  int(0)
126  ["key"]=>
127  int(0)
128}
129
130-- Iteration 1 --
131array(4) {
132  [1]=>
133  int(1)
134  ["value"]=>
135  int(1)
136  [0]=>
137  int(0)
138  ["key"]=>
139  int(0)
140}
141bool(false)
142array(4) {
143  [1]=>
144  int(1)
145  ["value"]=>
146  int(1)
147  [0]=>
148  int(0)
149  ["key"]=>
150  int(0)
151}
152
153-- Iteration 2 --
154array(4) {
155  [1]=>
156  int(-1)
157  ["value"]=>
158  int(-1)
159  [0]=>
160  int(0)
161  ["key"]=>
162  int(0)
163}
164bool(false)
165array(4) {
166  [1]=>
167  int(-1)
168  ["value"]=>
169  int(-1)
170  [0]=>
171  int(0)
172  ["key"]=>
173  int(0)
174}
175
176-- Iteration 3 --
177array(4) {
178  [1]=>
179  int(1)
180  ["value"]=>
181  int(1)
182  [0]=>
183  int(0)
184  ["key"]=>
185  int(0)
186}
187array(4) {
188  [1]=>
189  int(2)
190  ["value"]=>
191  int(2)
192  [0]=>
193  int(1)
194  ["key"]=>
195  int(1)
196}
197array(4) {
198  [1]=>
199  int(3)
200  ["value"]=>
201  int(3)
202  [0]=>
203  int(2)
204  ["key"]=>
205  int(2)
206}
207array(4) {
208  [1]=>
209  int(4)
210  ["value"]=>
211  int(4)
212  [0]=>
213  int(3)
214  ["key"]=>
215  int(3)
216}
217array(4) {
218  [1]=>
219  int(5)
220  ["value"]=>
221  int(5)
222  [0]=>
223  int(4)
224  ["key"]=>
225  int(4)
226}
227bool(false)
228array(4) {
229  [1]=>
230  int(1)
231  ["value"]=>
232  int(1)
233  [0]=>
234  int(0)
235  ["key"]=>
236  int(0)
237}
238
239-- Iteration 4 --
240array(4) {
241  [1]=>
242  int(-1)
243  ["value"]=>
244  int(-1)
245  [0]=>
246  int(0)
247  ["key"]=>
248  int(0)
249}
250array(4) {
251  [1]=>
252  int(-2)
253  ["value"]=>
254  int(-2)
255  [0]=>
256  int(1)
257  ["key"]=>
258  int(1)
259}
260array(4) {
261  [1]=>
262  int(-3)
263  ["value"]=>
264  int(-3)
265  [0]=>
266  int(2)
267  ["key"]=>
268  int(2)
269}
270array(4) {
271  [1]=>
272  int(6)
273  ["value"]=>
274  int(6)
275  [0]=>
276  int(3)
277  ["key"]=>
278  int(3)
279}
280array(4) {
281  [1]=>
282  int(7)
283  ["value"]=>
284  int(7)
285  [0]=>
286  int(4)
287  ["key"]=>
288  int(4)
289}
290array(4) {
291  [1]=>
292  int(8)
293  ["value"]=>
294  int(8)
295  [0]=>
296  int(5)
297  ["key"]=>
298  int(5)
299}
300bool(false)
301array(4) {
302  [1]=>
303  int(-1)
304  ["value"]=>
305  int(-1)
306  [0]=>
307  int(0)
308  ["key"]=>
309  int(0)
310}
311
312-- Iteration 5 --
313array(4) {
314  [1]=>
315  string(1) "a"
316  ["value"]=>
317  string(1) "a"
318  [0]=>
319  int(0)
320  ["key"]=>
321  int(0)
322}
323array(4) {
324  [1]=>
325  string(2) "ab"
326  ["value"]=>
327  string(2) "ab"
328  [0]=>
329  int(1)
330  ["key"]=>
331  int(1)
332}
333array(4) {
334  [1]=>
335  string(3) "abc"
336  ["value"]=>
337  string(3) "abc"
338  [0]=>
339  int(2)
340  ["key"]=>
341  int(2)
342}
343array(4) {
344  [1]=>
345  string(4) "abcd"
346  ["value"]=>
347  string(4) "abcd"
348  [0]=>
349  int(3)
350  ["key"]=>
351  int(3)
352}
353bool(false)
354array(4) {
355  [1]=>
356  string(1) "a"
357  ["value"]=>
358  string(1) "a"
359  [0]=>
360  int(0)
361  ["key"]=>
362  int(0)
363}
364
365-- Iteration 6 --
366array(4) {
367  [1]=>
368  string(3) "one"
369  ["value"]=>
370  string(3) "one"
371  [0]=>
372  int(1)
373  ["key"]=>
374  int(1)
375}
376array(4) {
377  [1]=>
378  string(3) "two"
379  ["value"]=>
380  string(3) "two"
381  [0]=>
382  int(2)
383  ["key"]=>
384  int(2)
385}
386array(4) {
387  [1]=>
388  string(5) "three"
389  ["value"]=>
390  string(5) "three"
391  [0]=>
392  int(3)
393  ["key"]=>
394  int(3)
395}
396array(4) {
397  [1]=>
398  string(4) "four"
399  ["value"]=>
400  string(4) "four"
401  [0]=>
402  int(4)
403  ["key"]=>
404  int(4)
405}
406bool(false)
407array(4) {
408  [1]=>
409  string(3) "one"
410  ["value"]=>
411  string(3) "one"
412  [0]=>
413  int(1)
414  ["key"]=>
415  int(1)
416}
417
418-- Iteration 7 --
419array(4) {
420  [1]=>
421  int(1)
422  ["value"]=>
423  int(1)
424  [0]=>
425  string(3) "one"
426  ["key"]=>
427  string(3) "one"
428}
429array(4) {
430  [1]=>
431  int(2)
432  ["value"]=>
433  int(2)
434  [0]=>
435  string(3) "two"
436  ["key"]=>
437  string(3) "two"
438}
439array(4) {
440  [1]=>
441  string(5) "three"
442  ["value"]=>
443  string(5) "three"
444  [0]=>
445  int(3)
446  ["key"]=>
447  int(3)
448}
449array(4) {
450  [1]=>
451  int(4)
452  ["value"]=>
453  int(4)
454  [0]=>
455  int(4)
456  ["key"]=>
457  int(4)
458}
459array(4) {
460  [1]=>
461  NULL
462  ["value"]=>
463  NULL
464  [0]=>
465  string(0) ""
466  ["key"]=>
467  string(0) ""
468}
469array(4) {
470  [1]=>
471  int(6)
472  ["value"]=>
473  int(6)
474  [0]=>
475  string(2) "  "
476  ["key"]=>
477  string(2) "  "
478}
479array(4) {
480  [1]=>
481  string(1) "�"
482  ["value"]=>
483  string(1) "�"
484  [0]=>
485  string(1) "�"
486  ["key"]=>
487  string(1) "�"
488}
489array(4) {
490  [1]=>
491  bool(true)
492  ["value"]=>
493  bool(true)
494  [0]=>
495  int(1)
496  ["key"]=>
497  int(1)
498}
499array(4) {
500  [1]=>
501  bool(false)
502  ["value"]=>
503  bool(false)
504  [0]=>
505  int(0)
506  ["key"]=>
507  int(0)
508}
509bool(false)
510array(4) {
511  [1]=>
512  int(1)
513  ["value"]=>
514  int(1)
515  [0]=>
516  string(3) "one"
517  ["key"]=>
518  string(3) "one"
519}
520
521-- Iteration 8 --
522array(4) {
523  [1]=>
524  string(5) "one.5"
525  ["value"]=>
526  string(5) "one.5"
527  [0]=>
528  string(3) "1.5"
529  ["key"]=>
530  string(3) "1.5"
531}
532array(4) {
533  [1]=>
534  string(11) "negative2.0"
535  ["value"]=>
536  string(11) "negative2.0"
537  [0]=>
538  string(4) "-2.0"
539  ["key"]=>
540  string(4) "-2.0"
541}
542bool(false)
543array(4) {
544  [1]=>
545  string(5) "one.5"
546  ["value"]=>
547  string(5) "one.5"
548  [0]=>
549  string(3) "1.5"
550  ["key"]=>
551  string(3) "1.5"
552}
553
554-- Iteration 9 --
555array(4) {
556  [1]=>
557  string(9) "negative5"
558  ["value"]=>
559  string(9) "negative5"
560  [0]=>
561  int(-5)
562  ["key"]=>
563  int(-5)
564}
565array(4) {
566  [1]=>
567  string(11) "negative.05"
568  ["value"]=>
569  string(11) "negative.05"
570  [0]=>
571  int(0)
572  ["key"]=>
573  int(0)
574}
575bool(false)
576array(4) {
577  [1]=>
578  string(9) "negative5"
579  ["value"]=>
580  string(9) "negative5"
581  [0]=>
582  int(-5)
583  ["key"]=>
584  int(-5)
585}
586
587
588*** Testing each() : possible variations ***
589-- Testing each() with reset() function --
590array(1) {
591  [0]=>
592  int(0)
593}
594array(4) {
595  [1]=>
596  array(1) {
597    [0]=>
598    int(0)
599  }
600  ["value"]=>
601  array(1) {
602    [0]=>
603    int(0)
604  }
605  [0]=>
606  int(0)
607  ["key"]=>
608  int(0)
609}
610array(4) {
611  [1]=>
612  int(1)
613  ["value"]=>
614  int(1)
615  [0]=>
616  int(0)
617  ["key"]=>
618  int(0)
619}
620-- Testing each() with resources --
621array(4) {
622  [1]=>
623  resource(%d) of type (stream)
624  ["value"]=>
625  resource(%d) of type (stream)
626  [0]=>
627  string(4) "file"
628  ["key"]=>
629  string(4) "file"
630}
631array(4) {
632  [1]=>
633  resource(%d) of type (stream)
634  ["value"]=>
635  resource(%d) of type (stream)
636  [0]=>
637  string(3) "dir"
638  ["key"]=>
639  string(3) "dir"
640}
641-- Testing each with objects --
642array(4) {
643  [1]=>
644  int(100)
645  ["value"]=>
646  int(100)
647  [0]=>
648  string(23) "�each_class�var_private"
649  ["key"]=>
650  string(23) "�each_class�var_private"
651}
652array(4) {
653  [1]=>
654  string(6) "string"
655  ["value"]=>
656  string(6) "string"
657  [0]=>
658  string(16) "�*�var_protected"
659  ["key"]=>
660  string(16) "�*�var_protected"
661}
662array(4) {
663  [1]=>
664  array(4) {
665    [0]=>
666    int(0)
667    [1]=>
668    int(1)
669    [2]=>
670    bool(true)
671    [3]=>
672    NULL
673  }
674  ["value"]=>
675  array(4) {
676    [0]=>
677    int(0)
678    [1]=>
679    int(1)
680    [2]=>
681    bool(true)
682    [3]=>
683    NULL
684  }
685  [0]=>
686  string(10) "var_public"
687  ["key"]=>
688  string(10) "var_public"
689}
690-- Testing each() with null array --
691bool(false)
692
693*** Testing error conditions ***
694
695Warning: each() expects exactly 1 parameter, 0 given in %s on line %d
696NULL
697
698Warning: each() expects exactly 1 parameter, 2 given in %s on line %d
699NULL
700
701Warning: Variable passed to each() is not an array or object in %s on line %d
702NULL
703
704Warning: Variable passed to each() is not an array or object in %s on line %d
705NULL
706
707Warning: Variable passed to each() is not an array or object in %s on line %d
708NULL
709Done
710