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