1--TEST--
2Test array_reverse() function : usage variations - different array values for 'array' argument
3--FILE--
4<?php
5/* Prototype  : array array_reverse(array $array [, bool $preserve_keys])
6 * Description: Return input as a new array with the order of the entries reversed
7 * Source code: ext/standard/array.c
8*/
9
10/*
11 * Testing the functionality of array_reverse() by giving
12 * different array values for $array argument
13*/
14
15echo "*** Testing array_reverse() : usage variations ***\n";
16
17//get an unset variable
18$unset_var = 10;
19unset ($unset_var);
20
21//get a resource variable
22$fp = fopen(__FILE__, "r");
23
24//get a class
25class classA
26{
27  public function __toString(){
28    return "Class A object";
29  }
30}
31
32// get a heredoc string
33$heredoc = <<<EOT
34Hello world
35EOT;
36
37$arrays = array (
38/*1*/  array(1, 2), // array with default keys and numeric values
39       array(1.1, 2.2), // array with default keys & float values
40       array( array(2), array(1)), // sub arrays
41       array(false,true), // array with default keys and boolean values
42       array(), // empty array
43       array(NULL), // array with NULL
44       array("a","aaaa","b","bbbb","c","ccccc"),
45
46       // associative arrays
47/*8*/  array(1 => "one", 2 => "two", 3 => "three"),  // explicit numeric keys, string values
48       array("one" => 1, "two" => 2, "three" => 3 ),  // string keys & numeric values
49       array( 1 => 10, 2 => 20, 4 => 40, 3 => 30),  // explicit numeric keys and numeric values
50       array( "one" => "ten", "two" => "twenty", "three" => "thirty"),  // string key/value
51       array("one" => 1, 2 => "two", 4 => "four"),  //mixed
52
53       // associative array, containing null/empty/boolean values as key/value
54/*13*/ array(NULL => "NULL", null => "null", "NULL" => NULL, "null" => null),
55       array(true => "true", false => "false", "false" => false, "true" => true),
56       array("" => "emptyd", '' => 'emptys', "emptyd" => "", 'emptys' => ''),
57       array(1 => '', 2 => "", 3 => NULL, 4 => null, 5 => false, 6 => true),
58       array('' => 1, "" => 2, NULL => 3, null => 4, false => 5, true => 6),
59
60       // array with repetative keys
61/*18*/ array("One" => 1, "two" => 2, "One" => 10, "two" => 20, "three" => 3)
62);
63
64// loop through the various elements of $arrays to test array_reverse()
65$iterator = 1;
66foreach($arrays as $array) {
67  echo "-- Iteration $iterator --\n";
68  // with default argument
69  echo "- with default argument -\n";
70  var_dump( array_reverse($array) );
71  // with all possible arguments
72  echo "- with \$preserve keys = true -\n";
73  var_dump( array_reverse($array, true) );
74  echo "- with \$preserve_keys = false -\n";
75  var_dump( array_reverse($array, false) );
76  $iterator++;
77};
78
79// close the file resource used
80fclose($fp);
81
82echo "Done";
83?>
84--EXPECT--
85*** Testing array_reverse() : usage variations ***
86-- Iteration 1 --
87- with default argument -
88array(2) {
89  [0]=>
90  int(2)
91  [1]=>
92  int(1)
93}
94- with $preserve keys = true -
95array(2) {
96  [1]=>
97  int(2)
98  [0]=>
99  int(1)
100}
101- with $preserve_keys = false -
102array(2) {
103  [0]=>
104  int(2)
105  [1]=>
106  int(1)
107}
108-- Iteration 2 --
109- with default argument -
110array(2) {
111  [0]=>
112  float(2.2)
113  [1]=>
114  float(1.1)
115}
116- with $preserve keys = true -
117array(2) {
118  [1]=>
119  float(2.2)
120  [0]=>
121  float(1.1)
122}
123- with $preserve_keys = false -
124array(2) {
125  [0]=>
126  float(2.2)
127  [1]=>
128  float(1.1)
129}
130-- Iteration 3 --
131- with default argument -
132array(2) {
133  [0]=>
134  array(1) {
135    [0]=>
136    int(1)
137  }
138  [1]=>
139  array(1) {
140    [0]=>
141    int(2)
142  }
143}
144- with $preserve keys = true -
145array(2) {
146  [1]=>
147  array(1) {
148    [0]=>
149    int(1)
150  }
151  [0]=>
152  array(1) {
153    [0]=>
154    int(2)
155  }
156}
157- with $preserve_keys = false -
158array(2) {
159  [0]=>
160  array(1) {
161    [0]=>
162    int(1)
163  }
164  [1]=>
165  array(1) {
166    [0]=>
167    int(2)
168  }
169}
170-- Iteration 4 --
171- with default argument -
172array(2) {
173  [0]=>
174  bool(true)
175  [1]=>
176  bool(false)
177}
178- with $preserve keys = true -
179array(2) {
180  [1]=>
181  bool(true)
182  [0]=>
183  bool(false)
184}
185- with $preserve_keys = false -
186array(2) {
187  [0]=>
188  bool(true)
189  [1]=>
190  bool(false)
191}
192-- Iteration 5 --
193- with default argument -
194array(0) {
195}
196- with $preserve keys = true -
197array(0) {
198}
199- with $preserve_keys = false -
200array(0) {
201}
202-- Iteration 6 --
203- with default argument -
204array(1) {
205  [0]=>
206  NULL
207}
208- with $preserve keys = true -
209array(1) {
210  [0]=>
211  NULL
212}
213- with $preserve_keys = false -
214array(1) {
215  [0]=>
216  NULL
217}
218-- Iteration 7 --
219- with default argument -
220array(6) {
221  [0]=>
222  string(5) "ccccc"
223  [1]=>
224  string(1) "c"
225  [2]=>
226  string(4) "bbbb"
227  [3]=>
228  string(1) "b"
229  [4]=>
230  string(4) "aaaa"
231  [5]=>
232  string(1) "a"
233}
234- with $preserve keys = true -
235array(6) {
236  [5]=>
237  string(5) "ccccc"
238  [4]=>
239  string(1) "c"
240  [3]=>
241  string(4) "bbbb"
242  [2]=>
243  string(1) "b"
244  [1]=>
245  string(4) "aaaa"
246  [0]=>
247  string(1) "a"
248}
249- with $preserve_keys = false -
250array(6) {
251  [0]=>
252  string(5) "ccccc"
253  [1]=>
254  string(1) "c"
255  [2]=>
256  string(4) "bbbb"
257  [3]=>
258  string(1) "b"
259  [4]=>
260  string(4) "aaaa"
261  [5]=>
262  string(1) "a"
263}
264-- Iteration 8 --
265- with default argument -
266array(3) {
267  [0]=>
268  string(5) "three"
269  [1]=>
270  string(3) "two"
271  [2]=>
272  string(3) "one"
273}
274- with $preserve keys = true -
275array(3) {
276  [3]=>
277  string(5) "three"
278  [2]=>
279  string(3) "two"
280  [1]=>
281  string(3) "one"
282}
283- with $preserve_keys = false -
284array(3) {
285  [0]=>
286  string(5) "three"
287  [1]=>
288  string(3) "two"
289  [2]=>
290  string(3) "one"
291}
292-- Iteration 9 --
293- with default argument -
294array(3) {
295  ["three"]=>
296  int(3)
297  ["two"]=>
298  int(2)
299  ["one"]=>
300  int(1)
301}
302- with $preserve keys = true -
303array(3) {
304  ["three"]=>
305  int(3)
306  ["two"]=>
307  int(2)
308  ["one"]=>
309  int(1)
310}
311- with $preserve_keys = false -
312array(3) {
313  ["three"]=>
314  int(3)
315  ["two"]=>
316  int(2)
317  ["one"]=>
318  int(1)
319}
320-- Iteration 10 --
321- with default argument -
322array(4) {
323  [0]=>
324  int(30)
325  [1]=>
326  int(40)
327  [2]=>
328  int(20)
329  [3]=>
330  int(10)
331}
332- with $preserve keys = true -
333array(4) {
334  [3]=>
335  int(30)
336  [4]=>
337  int(40)
338  [2]=>
339  int(20)
340  [1]=>
341  int(10)
342}
343- with $preserve_keys = false -
344array(4) {
345  [0]=>
346  int(30)
347  [1]=>
348  int(40)
349  [2]=>
350  int(20)
351  [3]=>
352  int(10)
353}
354-- Iteration 11 --
355- with default argument -
356array(3) {
357  ["three"]=>
358  string(6) "thirty"
359  ["two"]=>
360  string(6) "twenty"
361  ["one"]=>
362  string(3) "ten"
363}
364- with $preserve keys = true -
365array(3) {
366  ["three"]=>
367  string(6) "thirty"
368  ["two"]=>
369  string(6) "twenty"
370  ["one"]=>
371  string(3) "ten"
372}
373- with $preserve_keys = false -
374array(3) {
375  ["three"]=>
376  string(6) "thirty"
377  ["two"]=>
378  string(6) "twenty"
379  ["one"]=>
380  string(3) "ten"
381}
382-- Iteration 12 --
383- with default argument -
384array(3) {
385  [0]=>
386  string(4) "four"
387  [1]=>
388  string(3) "two"
389  ["one"]=>
390  int(1)
391}
392- with $preserve keys = true -
393array(3) {
394  [4]=>
395  string(4) "four"
396  [2]=>
397  string(3) "two"
398  ["one"]=>
399  int(1)
400}
401- with $preserve_keys = false -
402array(3) {
403  [0]=>
404  string(4) "four"
405  [1]=>
406  string(3) "two"
407  ["one"]=>
408  int(1)
409}
410-- Iteration 13 --
411- with default argument -
412array(3) {
413  ["null"]=>
414  NULL
415  ["NULL"]=>
416  NULL
417  [""]=>
418  string(4) "null"
419}
420- with $preserve keys = true -
421array(3) {
422  ["null"]=>
423  NULL
424  ["NULL"]=>
425  NULL
426  [""]=>
427  string(4) "null"
428}
429- with $preserve_keys = false -
430array(3) {
431  ["null"]=>
432  NULL
433  ["NULL"]=>
434  NULL
435  [""]=>
436  string(4) "null"
437}
438-- Iteration 14 --
439- with default argument -
440array(4) {
441  ["true"]=>
442  bool(true)
443  ["false"]=>
444  bool(false)
445  [0]=>
446  string(5) "false"
447  [1]=>
448  string(4) "true"
449}
450- with $preserve keys = true -
451array(4) {
452  ["true"]=>
453  bool(true)
454  ["false"]=>
455  bool(false)
456  [0]=>
457  string(5) "false"
458  [1]=>
459  string(4) "true"
460}
461- with $preserve_keys = false -
462array(4) {
463  ["true"]=>
464  bool(true)
465  ["false"]=>
466  bool(false)
467  [0]=>
468  string(5) "false"
469  [1]=>
470  string(4) "true"
471}
472-- Iteration 15 --
473- with default argument -
474array(3) {
475  ["emptys"]=>
476  string(0) ""
477  ["emptyd"]=>
478  string(0) ""
479  [""]=>
480  string(6) "emptys"
481}
482- with $preserve keys = true -
483array(3) {
484  ["emptys"]=>
485  string(0) ""
486  ["emptyd"]=>
487  string(0) ""
488  [""]=>
489  string(6) "emptys"
490}
491- with $preserve_keys = false -
492array(3) {
493  ["emptys"]=>
494  string(0) ""
495  ["emptyd"]=>
496  string(0) ""
497  [""]=>
498  string(6) "emptys"
499}
500-- Iteration 16 --
501- with default argument -
502array(6) {
503  [0]=>
504  bool(true)
505  [1]=>
506  bool(false)
507  [2]=>
508  NULL
509  [3]=>
510  NULL
511  [4]=>
512  string(0) ""
513  [5]=>
514  string(0) ""
515}
516- with $preserve keys = true -
517array(6) {
518  [6]=>
519  bool(true)
520  [5]=>
521  bool(false)
522  [4]=>
523  NULL
524  [3]=>
525  NULL
526  [2]=>
527  string(0) ""
528  [1]=>
529  string(0) ""
530}
531- with $preserve_keys = false -
532array(6) {
533  [0]=>
534  bool(true)
535  [1]=>
536  bool(false)
537  [2]=>
538  NULL
539  [3]=>
540  NULL
541  [4]=>
542  string(0) ""
543  [5]=>
544  string(0) ""
545}
546-- Iteration 17 --
547- with default argument -
548array(3) {
549  [0]=>
550  int(6)
551  [1]=>
552  int(5)
553  [""]=>
554  int(4)
555}
556- with $preserve keys = true -
557array(3) {
558  [1]=>
559  int(6)
560  [0]=>
561  int(5)
562  [""]=>
563  int(4)
564}
565- with $preserve_keys = false -
566array(3) {
567  [0]=>
568  int(6)
569  [1]=>
570  int(5)
571  [""]=>
572  int(4)
573}
574-- Iteration 18 --
575- with default argument -
576array(3) {
577  ["three"]=>
578  int(3)
579  ["two"]=>
580  int(20)
581  ["One"]=>
582  int(10)
583}
584- with $preserve keys = true -
585array(3) {
586  ["three"]=>
587  int(3)
588  ["two"]=>
589  int(20)
590  ["One"]=>
591  int(10)
592}
593- with $preserve_keys = false -
594array(3) {
595  ["three"]=>
596  int(3)
597  ["two"]=>
598  int(20)
599  ["One"]=>
600  int(10)
601}
602Done
603