array_unshift_variation6.phpt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. --TEST--
  2. Test array_unshift() function : usage variations - two dimensional arrays for 'array' argument
  3. --FILE--
  4. <?php
  5. /* Prototype : int array_unshift(array $array, mixed $var [, mixed ...])
  6. * Description: Pushes elements onto the beginning of the array
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Testing the functionality of array_unshift() by giving two-dimensional
  11. * arrays and also sub-arrays within the two-dimensional array for $array argument.
  12. * The $var argument passed is a fixed value
  13. */
  14. echo "*** Testing array_unshift() : two dimensional arrays for \$array argument ***\n";
  15. // initializing $var argument
  16. $var = 10;
  17. // two-dimensional array to be passed to $array argument
  18. $two_dimensional_array = array(
  19. // associative array
  20. array('color' => 'red', 'item' => 'pen', 'place' => 'LA'),
  21. // numeric array
  22. array(1, 2, 3, 4, 5),
  23. // combination of numeric and associative arrays
  24. array('a' => 'green', 'red', 'brown', 33, 88, 'orange', 'item' => 'ball')
  25. );
  26. /* Passing the entire $two_dimensional_array to $array */
  27. /* With default argument */
  28. // returns element count in the resulting array after arguments are pushed to
  29. // beginning of the given array
  30. $temp_array = $two_dimensional_array;
  31. var_dump( array_unshift($temp_array, $var) ); // whole 2-d array
  32. // dumps the resulting array
  33. var_dump($temp_array);
  34. /* With optional arguments */
  35. // returns element count in the resulting array after arguments are pushed to
  36. // beginning of the given array
  37. $temp_array = $two_dimensional_array;
  38. var_dump( array_unshift($temp_array, $var, "hello", 'world') ); // whole 2-d array
  39. // dumps the resulting array
  40. var_dump($temp_array);
  41. /* Passing the sub-array within the $two_dimensional_array to $array argument */
  42. /* With default argument */
  43. // returns element count in the resulting array after arguments are pushed to
  44. // beginning of the given array
  45. $temp_array = $two_dimensional_array[0];
  46. var_dump( array_unshift($temp_array, $var) ); // sub array
  47. // dumps the resulting array
  48. var_dump($temp_array);
  49. /* With optional arguments */
  50. // returns element count in the resulting array after arguments are pushed to
  51. // beginning of the given array
  52. $temp_array = $two_dimensional_array[0];
  53. var_dump( array_unshift($temp_array, $var, "hello", 'world') ); // sub array
  54. // dumps the resulting array
  55. var_dump($temp_array);
  56. echo "Done";
  57. ?>
  58. --EXPECTF--
  59. *** Testing array_unshift() : two dimensional arrays for $array argument ***
  60. int(4)
  61. array(4) {
  62. [0]=>
  63. int(10)
  64. [1]=>
  65. array(3) {
  66. ["color"]=>
  67. string(3) "red"
  68. ["item"]=>
  69. string(3) "pen"
  70. ["place"]=>
  71. string(2) "LA"
  72. }
  73. [2]=>
  74. array(5) {
  75. [0]=>
  76. int(1)
  77. [1]=>
  78. int(2)
  79. [2]=>
  80. int(3)
  81. [3]=>
  82. int(4)
  83. [4]=>
  84. int(5)
  85. }
  86. [3]=>
  87. array(7) {
  88. ["a"]=>
  89. string(5) "green"
  90. [0]=>
  91. string(3) "red"
  92. [1]=>
  93. string(5) "brown"
  94. [2]=>
  95. int(33)
  96. [3]=>
  97. int(88)
  98. [4]=>
  99. string(6) "orange"
  100. ["item"]=>
  101. string(4) "ball"
  102. }
  103. }
  104. int(6)
  105. array(6) {
  106. [0]=>
  107. int(10)
  108. [1]=>
  109. string(5) "hello"
  110. [2]=>
  111. string(5) "world"
  112. [3]=>
  113. array(3) {
  114. ["color"]=>
  115. string(3) "red"
  116. ["item"]=>
  117. string(3) "pen"
  118. ["place"]=>
  119. string(2) "LA"
  120. }
  121. [4]=>
  122. array(5) {
  123. [0]=>
  124. int(1)
  125. [1]=>
  126. int(2)
  127. [2]=>
  128. int(3)
  129. [3]=>
  130. int(4)
  131. [4]=>
  132. int(5)
  133. }
  134. [5]=>
  135. array(7) {
  136. ["a"]=>
  137. string(5) "green"
  138. [0]=>
  139. string(3) "red"
  140. [1]=>
  141. string(5) "brown"
  142. [2]=>
  143. int(33)
  144. [3]=>
  145. int(88)
  146. [4]=>
  147. string(6) "orange"
  148. ["item"]=>
  149. string(4) "ball"
  150. }
  151. }
  152. int(4)
  153. array(4) {
  154. [0]=>
  155. int(10)
  156. ["color"]=>
  157. string(3) "red"
  158. ["item"]=>
  159. string(3) "pen"
  160. ["place"]=>
  161. string(2) "LA"
  162. }
  163. int(6)
  164. array(6) {
  165. [0]=>
  166. int(10)
  167. [1]=>
  168. string(5) "hello"
  169. [2]=>
  170. string(5) "world"
  171. ["color"]=>
  172. string(3) "red"
  173. ["item"]=>
  174. string(3) "pen"
  175. ["place"]=>
  176. string(2) "LA"
  177. }
  178. Done