array_map_variation3.phpt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. --TEST--
  2. Test array_map() function : usage variations - different arrays for 'arr1' argument
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_map ( callback $callback , array $arr1 [, array $... ] )
  6. * Description: Applies the callback to the elements of the given arrays
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Test array_map() by passing different arrays for $arr1 argument
  11. */
  12. echo "*** Testing array_map() : different arrays for 'arr1' argument ***\n";
  13. function callback($a)
  14. {
  15. return ($a);
  16. }
  17. // different arrays
  18. $arrays = array (
  19. /*1*/ array(1, 2), // array with default keys and numeric values
  20. array(1.1, 2.2), // array with default keys & float values
  21. array( array(2), array(1)), // sub arrays
  22. array(false,true), // array with default keys and boolean values
  23. array(), // empty array
  24. array(NULL), // array with NULL
  25. array("a","aaaa","b","bbbb","c","ccccc"),
  26. // associative arrays
  27. /*8*/ array(1 => "one", 2 => "two", 3 => "three"), // explicit numeric keys, string values
  28. array("one" => 1, "two" => 2, "three" => 3 ), // string keys & numeric values
  29. array( 1 => 10, 2 => 20, 4 => 40, 3 => 30), // explicit numeric keys and numeric values
  30. array( "one" => "ten", "two" => "twenty", "three" => "thirty"), // string key/value
  31. array("one" => 1, 2 => "two", 4 => "four"), //mixed
  32. // associative array, containing null/empty/boolean values as key/value
  33. /*13*/ array(NULL => "NULL", null => "null", "NULL" => NULL, "null" => null),
  34. array(true => "true", false => "false", "false" => false, "true" => true),
  35. array("" => "emptyd", '' => 'emptys', "emptyd" => "", 'emptys' => ''),
  36. array(1 => '', 2 => "", 3 => NULL, 4 => null, 5 => false, 6 => true),
  37. array('' => 1, "" => 2, NULL => 3, null => 4, false => 5, true => 6),
  38. // array with repetative keys
  39. /*18*/ array("One" => 1, "two" => 2, "One" => 10, "two" => 20, "three" => 3)
  40. );
  41. // loop through the various elements of $arrays to test array_map()
  42. $iterator = 1;
  43. foreach($arrays as $arr1) {
  44. echo "-- Iteration $iterator --\n";
  45. var_dump( array_map('callback', $arr1) );
  46. $iterator++;
  47. }
  48. echo "Done";
  49. ?>
  50. --EXPECTF--
  51. *** Testing array_map() : different arrays for 'arr1' argument ***
  52. -- Iteration 1 --
  53. array(2) {
  54. [0]=>
  55. int(1)
  56. [1]=>
  57. int(2)
  58. }
  59. -- Iteration 2 --
  60. array(2) {
  61. [0]=>
  62. float(1.1)
  63. [1]=>
  64. float(2.2)
  65. }
  66. -- Iteration 3 --
  67. array(2) {
  68. [0]=>
  69. array(1) {
  70. [0]=>
  71. int(2)
  72. }
  73. [1]=>
  74. array(1) {
  75. [0]=>
  76. int(1)
  77. }
  78. }
  79. -- Iteration 4 --
  80. array(2) {
  81. [0]=>
  82. bool(false)
  83. [1]=>
  84. bool(true)
  85. }
  86. -- Iteration 5 --
  87. array(0) {
  88. }
  89. -- Iteration 6 --
  90. array(1) {
  91. [0]=>
  92. NULL
  93. }
  94. -- Iteration 7 --
  95. array(6) {
  96. [0]=>
  97. string(1) "a"
  98. [1]=>
  99. string(4) "aaaa"
  100. [2]=>
  101. string(1) "b"
  102. [3]=>
  103. string(4) "bbbb"
  104. [4]=>
  105. string(1) "c"
  106. [5]=>
  107. string(5) "ccccc"
  108. }
  109. -- Iteration 8 --
  110. array(3) {
  111. [1]=>
  112. string(3) "one"
  113. [2]=>
  114. string(3) "two"
  115. [3]=>
  116. string(5) "three"
  117. }
  118. -- Iteration 9 --
  119. array(3) {
  120. ["one"]=>
  121. int(1)
  122. ["two"]=>
  123. int(2)
  124. ["three"]=>
  125. int(3)
  126. }
  127. -- Iteration 10 --
  128. array(4) {
  129. [1]=>
  130. int(10)
  131. [2]=>
  132. int(20)
  133. [4]=>
  134. int(40)
  135. [3]=>
  136. int(30)
  137. }
  138. -- Iteration 11 --
  139. array(3) {
  140. ["one"]=>
  141. string(3) "ten"
  142. ["two"]=>
  143. string(6) "twenty"
  144. ["three"]=>
  145. string(6) "thirty"
  146. }
  147. -- Iteration 12 --
  148. array(3) {
  149. ["one"]=>
  150. int(1)
  151. [2]=>
  152. string(3) "two"
  153. [4]=>
  154. string(4) "four"
  155. }
  156. -- Iteration 13 --
  157. array(3) {
  158. [""]=>
  159. string(4) "null"
  160. ["NULL"]=>
  161. NULL
  162. ["null"]=>
  163. NULL
  164. }
  165. -- Iteration 14 --
  166. array(4) {
  167. [1]=>
  168. string(4) "true"
  169. [0]=>
  170. string(5) "false"
  171. ["false"]=>
  172. bool(false)
  173. ["true"]=>
  174. bool(true)
  175. }
  176. -- Iteration 15 --
  177. array(3) {
  178. [""]=>
  179. string(6) "emptys"
  180. ["emptyd"]=>
  181. string(0) ""
  182. ["emptys"]=>
  183. string(0) ""
  184. }
  185. -- Iteration 16 --
  186. array(6) {
  187. [1]=>
  188. string(0) ""
  189. [2]=>
  190. string(0) ""
  191. [3]=>
  192. NULL
  193. [4]=>
  194. NULL
  195. [5]=>
  196. bool(false)
  197. [6]=>
  198. bool(true)
  199. }
  200. -- Iteration 17 --
  201. array(3) {
  202. [""]=>
  203. int(4)
  204. [0]=>
  205. int(5)
  206. [1]=>
  207. int(6)
  208. }
  209. -- Iteration 18 --
  210. array(3) {
  211. ["One"]=>
  212. int(10)
  213. ["two"]=>
  214. int(20)
  215. ["three"]=>
  216. int(3)
  217. }
  218. Done