123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- --TEST--
- Test array_unshift() function : usage variations - different array values for 'array' argument
- --FILE--
- <?php
- /* Prototype : int array_unshift(array $array, mixed $var [, mixed ...])
- * Description: Pushes elements onto the beginning of the array
- * Source code: ext/standard/array.c
- */
- /*
- * Testing the behavior of array_unshift() by passing different types of arrays
- * to $array argument to which the $var arguments will be prepended
- */
- echo "*** Testing array_unshift() : different arrays for \$array argument ***\n";
- // initialize $var argument
- $var = 10;
- // different arrays to be passed to $array argument
- $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_unshift()
- $iterator = 1;
- foreach($arrays as $array) {
- echo "-- Iteration $iterator --\n";
- /* with default argument */
- // returns element count in the resulting array after arguments are pushed to
- // beginning of the given array
- $temp_array = $array;
- var_dump( array_unshift($temp_array, $var) );
- // dump the resulting array
- var_dump($temp_array);
- /* with optional arguments */
- // returns element count in the resulting array after arguments are pushed to
- // beginning of the given array
- $temp_array = $array;
- var_dump( array_unshift($temp_array, $var, "hello", 'world') );
- // dump the resulting array
- var_dump($temp_array);
- $iterator++;
- }
- echo "Done";
- ?>
- --EXPECTF--
- *** Testing array_unshift() : different arrays for $array argument ***
- -- Iteration 1 --
- int(3)
- array(3) {
- [0]=>
- int(10)
- [1]=>
- int(1)
- [2]=>
- int(2)
- }
- int(5)
- array(5) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- int(1)
- [4]=>
- int(2)
- }
- -- Iteration 2 --
- int(3)
- array(3) {
- [0]=>
- int(10)
- [1]=>
- float(1.1)
- [2]=>
- float(2.2)
- }
- int(5)
- array(5) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- float(1.1)
- [4]=>
- float(2.2)
- }
- -- Iteration 3 --
- int(3)
- array(3) {
- [0]=>
- int(10)
- [1]=>
- array(1) {
- [0]=>
- int(2)
- }
- [2]=>
- array(1) {
- [0]=>
- int(1)
- }
- }
- int(5)
- array(5) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- array(1) {
- [0]=>
- int(2)
- }
- [4]=>
- array(1) {
- [0]=>
- int(1)
- }
- }
- -- Iteration 4 --
- int(3)
- array(3) {
- [0]=>
- int(10)
- [1]=>
- bool(false)
- [2]=>
- bool(true)
- }
- int(5)
- array(5) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- bool(false)
- [4]=>
- bool(true)
- }
- -- Iteration 5 --
- int(1)
- array(1) {
- [0]=>
- int(10)
- }
- int(3)
- array(3) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- }
- -- Iteration 6 --
- int(2)
- array(2) {
- [0]=>
- int(10)
- [1]=>
- NULL
- }
- int(4)
- array(4) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- NULL
- }
- -- Iteration 7 --
- int(7)
- array(7) {
- [0]=>
- int(10)
- [1]=>
- string(1) "a"
- [2]=>
- string(4) "aaaa"
- [3]=>
- string(1) "b"
- [4]=>
- string(4) "bbbb"
- [5]=>
- string(1) "c"
- [6]=>
- string(5) "ccccc"
- }
- int(9)
- array(9) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- string(1) "a"
- [4]=>
- string(4) "aaaa"
- [5]=>
- string(1) "b"
- [6]=>
- string(4) "bbbb"
- [7]=>
- string(1) "c"
- [8]=>
- string(5) "ccccc"
- }
- -- Iteration 8 --
- int(4)
- array(4) {
- [0]=>
- int(10)
- [1]=>
- string(3) "one"
- [2]=>
- string(3) "two"
- [3]=>
- string(5) "three"
- }
- int(6)
- array(6) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- string(3) "one"
- [4]=>
- string(3) "two"
- [5]=>
- string(5) "three"
- }
- -- Iteration 9 --
- int(4)
- array(4) {
- [0]=>
- int(10)
- ["one"]=>
- int(1)
- ["two"]=>
- int(2)
- ["three"]=>
- int(3)
- }
- int(6)
- array(6) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- ["one"]=>
- int(1)
- ["two"]=>
- int(2)
- ["three"]=>
- int(3)
- }
- -- Iteration 10 --
- int(5)
- array(5) {
- [0]=>
- int(10)
- [1]=>
- int(10)
- [2]=>
- int(20)
- [3]=>
- int(40)
- [4]=>
- int(30)
- }
- int(7)
- array(7) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- int(10)
- [4]=>
- int(20)
- [5]=>
- int(40)
- [6]=>
- int(30)
- }
- -- Iteration 11 --
- int(4)
- array(4) {
- [0]=>
- int(10)
- ["one"]=>
- string(3) "ten"
- ["two"]=>
- string(6) "twenty"
- ["three"]=>
- string(6) "thirty"
- }
- int(6)
- array(6) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- ["one"]=>
- string(3) "ten"
- ["two"]=>
- string(6) "twenty"
- ["three"]=>
- string(6) "thirty"
- }
- -- Iteration 12 --
- int(4)
- array(4) {
- [0]=>
- int(10)
- ["one"]=>
- int(1)
- [1]=>
- string(3) "two"
- [2]=>
- string(4) "four"
- }
- int(6)
- array(6) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- ["one"]=>
- int(1)
- [3]=>
- string(3) "two"
- [4]=>
- string(4) "four"
- }
- -- Iteration 13 --
- int(4)
- array(4) {
- [0]=>
- int(10)
- [""]=>
- string(4) "null"
- ["NULL"]=>
- NULL
- ["null"]=>
- NULL
- }
- int(6)
- array(6) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [""]=>
- string(4) "null"
- ["NULL"]=>
- NULL
- ["null"]=>
- NULL
- }
- -- Iteration 14 --
- int(5)
- array(5) {
- [0]=>
- int(10)
- [1]=>
- string(4) "true"
- [2]=>
- string(5) "false"
- ["false"]=>
- bool(false)
- ["true"]=>
- bool(true)
- }
- int(7)
- array(7) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- string(4) "true"
- [4]=>
- string(5) "false"
- ["false"]=>
- bool(false)
- ["true"]=>
- bool(true)
- }
- -- Iteration 15 --
- int(4)
- array(4) {
- [0]=>
- int(10)
- [""]=>
- string(6) "emptys"
- ["emptyd"]=>
- string(0) ""
- ["emptys"]=>
- string(0) ""
- }
- int(6)
- array(6) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [""]=>
- string(6) "emptys"
- ["emptyd"]=>
- string(0) ""
- ["emptys"]=>
- string(0) ""
- }
- -- Iteration 16 --
- int(7)
- array(7) {
- [0]=>
- int(10)
- [1]=>
- string(0) ""
- [2]=>
- string(0) ""
- [3]=>
- NULL
- [4]=>
- NULL
- [5]=>
- bool(false)
- [6]=>
- bool(true)
- }
- int(9)
- array(9) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [3]=>
- string(0) ""
- [4]=>
- string(0) ""
- [5]=>
- NULL
- [6]=>
- NULL
- [7]=>
- bool(false)
- [8]=>
- bool(true)
- }
- -- Iteration 17 --
- int(4)
- array(4) {
- [0]=>
- int(10)
- [""]=>
- int(4)
- [1]=>
- int(5)
- [2]=>
- int(6)
- }
- int(6)
- array(6) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- [""]=>
- int(4)
- [3]=>
- int(5)
- [4]=>
- int(6)
- }
- -- Iteration 18 --
- int(4)
- array(4) {
- [0]=>
- int(10)
- ["One"]=>
- int(10)
- ["two"]=>
- int(20)
- ["three"]=>
- int(3)
- }
- int(6)
- array(6) {
- [0]=>
- int(10)
- [1]=>
- string(5) "hello"
- [2]=>
- string(5) "world"
- ["One"]=>
- int(10)
- ["two"]=>
- int(20)
- ["three"]=>
- int(3)
- }
- Done
|