123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- --TEST--
- Test array_reverse() function : usage variations - different array values for 'array' argument
- --FILE--
- <?php
- /* Prototype : array array_reverse(array $array [, bool $preserve_keys])
- * Description: Return input as a new array with the order of the entries reversed
- * Source code: ext/standard/array.c
- */
- /*
- * Testing the functionality of array_reverse() by giving
- * different array values for $array argument
- */
- echo "*** Testing array_reverse() : usage variations ***\n";
- //get an unset variable
- $unset_var = 10;
- unset ($unset_var);
- //get a resource variable
- $fp = fopen(__FILE__, "r");
- //get a class
- class classA
- {
- public function __toString(){
- return "Class A object";
- }
- }
- // get a heredoc string
- $heredoc = <<<EOT
- Hello world
- EOT;
- $arrays = array (
- /*1*/ array(1, 2), // array with default keys and numeric values
- array(1.1, 2.2), // array with default keys & float values
- array( array(2), array(1)), // sub arrays
- array(false,true), // array with default keys and boolean values
- array(), // empty array
- array(NULL), // array with NULL
- array("a","aaaa","b","bbbb","c","ccccc"),
- // associative arrays
- /*8*/ array(1 => "one", 2 => "two", 3 => "three"), // explicit numeric keys, string values
- array("one" => 1, "two" => 2, "three" => 3 ), // string keys & numeric values
- array( 1 => 10, 2 => 20, 4 => 40, 3 => 30), // explicit numeric keys and numeric values
- array( "one" => "ten", "two" => "twenty", "three" => "thirty"), // string key/value
- array("one" => 1, 2 => "two", 4 => "four"), //mixed
- // associative array, containing null/empty/boolean values as key/value
- /*13*/ array(NULL => "NULL", null => "null", "NULL" => NULL, "null" => null),
- array(true => "true", false => "false", "false" => false, "true" => true),
- array("" => "emptyd", '' => 'emptys', "emptyd" => "", 'emptys' => ''),
- array(1 => '', 2 => "", 3 => NULL, 4 => null, 5 => false, 6 => true),
- array('' => 1, "" => 2, NULL => 3, null => 4, false => 5, true => 6),
- // array with repetative keys
- /*18*/ array("One" => 1, "two" => 2, "One" => 10, "two" => 20, "three" => 3)
- );
- // loop through the various elements of $arrays to test array_reverse()
- $iterator = 1;
- foreach($arrays as $array) {
- echo "-- Iteration $iterator --\n";
- // with default argument
- echo "- with default argument -\n";
- var_dump( array_reverse($array) );
- // with all possible arguments
- echo "- with \$preserve keys = true -\n";
- var_dump( array_reverse($array, true) );
- echo "- with \$preserve_keys = false -\n";
- var_dump( array_reverse($array, false) );
- $iterator++;
- };
- // close the file resource used
- fclose($fp);
- echo "Done";
- ?>
- --EXPECTF--
- *** Testing array_reverse() : usage variations ***
- -- Iteration 1 --
- - with default argument -
- array(2) {
- [0]=>
- int(2)
- [1]=>
- int(1)
- }
- - with $preserve keys = true -
- array(2) {
- [1]=>
- int(2)
- [0]=>
- int(1)
- }
- - with $preserve_keys = false -
- array(2) {
- [0]=>
- int(2)
- [1]=>
- int(1)
- }
- -- Iteration 2 --
- - with default argument -
- array(2) {
- [0]=>
- float(2.2)
- [1]=>
- float(1.1)
- }
- - with $preserve keys = true -
- array(2) {
- [1]=>
- float(2.2)
- [0]=>
- float(1.1)
- }
- - with $preserve_keys = false -
- array(2) {
- [0]=>
- float(2.2)
- [1]=>
- float(1.1)
- }
- -- Iteration 3 --
- - with default argument -
- array(2) {
- [0]=>
- array(1) {
- [0]=>
- int(1)
- }
- [1]=>
- array(1) {
- [0]=>
- int(2)
- }
- }
- - with $preserve keys = true -
- array(2) {
- [1]=>
- array(1) {
- [0]=>
- int(1)
- }
- [0]=>
- array(1) {
- [0]=>
- int(2)
- }
- }
- - with $preserve_keys = false -
- array(2) {
- [0]=>
- array(1) {
- [0]=>
- int(1)
- }
- [1]=>
- array(1) {
- [0]=>
- int(2)
- }
- }
- -- Iteration 4 --
- - with default argument -
- array(2) {
- [0]=>
- bool(true)
- [1]=>
- bool(false)
- }
- - with $preserve keys = true -
- array(2) {
- [1]=>
- bool(true)
- [0]=>
- bool(false)
- }
- - with $preserve_keys = false -
- array(2) {
- [0]=>
- bool(true)
- [1]=>
- bool(false)
- }
- -- Iteration 5 --
- - with default argument -
- array(0) {
- }
- - with $preserve keys = true -
- array(0) {
- }
- - with $preserve_keys = false -
- array(0) {
- }
- -- Iteration 6 --
- - with default argument -
- array(1) {
- [0]=>
- NULL
- }
- - with $preserve keys = true -
- array(1) {
- [0]=>
- NULL
- }
- - with $preserve_keys = false -
- array(1) {
- [0]=>
- NULL
- }
- -- Iteration 7 --
- - with default argument -
- array(6) {
- [0]=>
- string(5) "ccccc"
- [1]=>
- string(1) "c"
- [2]=>
- string(4) "bbbb"
- [3]=>
- string(1) "b"
- [4]=>
- string(4) "aaaa"
- [5]=>
- string(1) "a"
- }
- - with $preserve keys = true -
- array(6) {
- [5]=>
- string(5) "ccccc"
- [4]=>
- string(1) "c"
- [3]=>
- string(4) "bbbb"
- [2]=>
- string(1) "b"
- [1]=>
- string(4) "aaaa"
- [0]=>
- string(1) "a"
- }
- - with $preserve_keys = false -
- array(6) {
- [0]=>
- string(5) "ccccc"
- [1]=>
- string(1) "c"
- [2]=>
- string(4) "bbbb"
- [3]=>
- string(1) "b"
- [4]=>
- string(4) "aaaa"
- [5]=>
- string(1) "a"
- }
- -- Iteration 8 --
- - with default argument -
- array(3) {
- [0]=>
- string(5) "three"
- [1]=>
- string(3) "two"
- [2]=>
- string(3) "one"
- }
- - with $preserve keys = true -
- array(3) {
- [3]=>
- string(5) "three"
- [2]=>
- string(3) "two"
- [1]=>
- string(3) "one"
- }
- - with $preserve_keys = false -
- array(3) {
- [0]=>
- string(5) "three"
- [1]=>
- string(3) "two"
- [2]=>
- string(3) "one"
- }
- -- Iteration 9 --
- - with default argument -
- array(3) {
- ["three"]=>
- int(3)
- ["two"]=>
- int(2)
- ["one"]=>
- int(1)
- }
- - with $preserve keys = true -
- array(3) {
- ["three"]=>
- int(3)
- ["two"]=>
- int(2)
- ["one"]=>
- int(1)
- }
- - with $preserve_keys = false -
- array(3) {
- ["three"]=>
- int(3)
- ["two"]=>
- int(2)
- ["one"]=>
- int(1)
- }
- -- Iteration 10 --
- - with default argument -
- array(4) {
- [0]=>
- int(30)
- [1]=>
- int(40)
- [2]=>
- int(20)
- [3]=>
- int(10)
- }
- - with $preserve keys = true -
- array(4) {
- [3]=>
- int(30)
- [4]=>
- int(40)
- [2]=>
- int(20)
- [1]=>
- int(10)
- }
- - with $preserve_keys = false -
- array(4) {
- [0]=>
- int(30)
- [1]=>
- int(40)
- [2]=>
- int(20)
- [3]=>
- int(10)
- }
- -- Iteration 11 --
- - with default argument -
- array(3) {
- ["three"]=>
- string(6) "thirty"
- ["two"]=>
- string(6) "twenty"
- ["one"]=>
- string(3) "ten"
- }
- - with $preserve keys = true -
- array(3) {
- ["three"]=>
- string(6) "thirty"
- ["two"]=>
- string(6) "twenty"
- ["one"]=>
- string(3) "ten"
- }
- - with $preserve_keys = false -
- array(3) {
- ["three"]=>
- string(6) "thirty"
- ["two"]=>
- string(6) "twenty"
- ["one"]=>
- string(3) "ten"
- }
- -- Iteration 12 --
- - with default argument -
- array(3) {
- [0]=>
- string(4) "four"
- [1]=>
- string(3) "two"
- ["one"]=>
- int(1)
- }
- - with $preserve keys = true -
- array(3) {
- [4]=>
- string(4) "four"
- [2]=>
- string(3) "two"
- ["one"]=>
- int(1)
- }
- - with $preserve_keys = false -
- array(3) {
- [0]=>
- string(4) "four"
- [1]=>
- string(3) "two"
- ["one"]=>
- int(1)
- }
- -- Iteration 13 --
- - with default argument -
- array(3) {
- ["null"]=>
- NULL
- ["NULL"]=>
- NULL
- [""]=>
- string(4) "null"
- }
- - with $preserve keys = true -
- array(3) {
- ["null"]=>
- NULL
- ["NULL"]=>
- NULL
- [""]=>
- string(4) "null"
- }
- - with $preserve_keys = false -
- array(3) {
- ["null"]=>
- NULL
- ["NULL"]=>
- NULL
- [""]=>
- string(4) "null"
- }
- -- Iteration 14 --
- - with default argument -
- array(4) {
- ["true"]=>
- bool(true)
- ["false"]=>
- bool(false)
- [0]=>
- string(5) "false"
- [1]=>
- string(4) "true"
- }
- - with $preserve keys = true -
- array(4) {
- ["true"]=>
- bool(true)
- ["false"]=>
- bool(false)
- [0]=>
- string(5) "false"
- [1]=>
- string(4) "true"
- }
- - with $preserve_keys = false -
- array(4) {
- ["true"]=>
- bool(true)
- ["false"]=>
- bool(false)
- [0]=>
- string(5) "false"
- [1]=>
- string(4) "true"
- }
- -- Iteration 15 --
- - with default argument -
- array(3) {
- ["emptys"]=>
- string(0) ""
- ["emptyd"]=>
- string(0) ""
- [""]=>
- string(6) "emptys"
- }
- - with $preserve keys = true -
- array(3) {
- ["emptys"]=>
- string(0) ""
- ["emptyd"]=>
- string(0) ""
- [""]=>
- string(6) "emptys"
- }
- - with $preserve_keys = false -
- array(3) {
- ["emptys"]=>
- string(0) ""
- ["emptyd"]=>
- string(0) ""
- [""]=>
- string(6) "emptys"
- }
- -- Iteration 16 --
- - with default argument -
- array(6) {
- [0]=>
- bool(true)
- [1]=>
- bool(false)
- [2]=>
- NULL
- [3]=>
- NULL
- [4]=>
- string(0) ""
- [5]=>
- string(0) ""
- }
- - with $preserve keys = true -
- array(6) {
- [6]=>
- bool(true)
- [5]=>
- bool(false)
- [4]=>
- NULL
- [3]=>
- NULL
- [2]=>
- string(0) ""
- [1]=>
- string(0) ""
- }
- - with $preserve_keys = false -
- array(6) {
- [0]=>
- bool(true)
- [1]=>
- bool(false)
- [2]=>
- NULL
- [3]=>
- NULL
- [4]=>
- string(0) ""
- [5]=>
- string(0) ""
- }
- -- Iteration 17 --
- - with default argument -
- array(3) {
- [0]=>
- int(6)
- [1]=>
- int(5)
- [""]=>
- int(4)
- }
- - with $preserve keys = true -
- array(3) {
- [1]=>
- int(6)
- [0]=>
- int(5)
- [""]=>
- int(4)
- }
- - with $preserve_keys = false -
- array(3) {
- [0]=>
- int(6)
- [1]=>
- int(5)
- [""]=>
- int(4)
- }
- -- Iteration 18 --
- - with default argument -
- array(3) {
- ["three"]=>
- int(3)
- ["two"]=>
- int(20)
- ["One"]=>
- int(10)
- }
- - with $preserve keys = true -
- array(3) {
- ["three"]=>
- int(3)
- ["two"]=>
- int(20)
- ["One"]=>
- int(10)
- }
- - with $preserve_keys = false -
- array(3) {
- ["three"]=>
- int(3)
- ["two"]=>
- int(20)
- ["One"]=>
- int(10)
- }
- Done
|