array_combine_variation3.phpt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. --TEST--
  2. Test array_combine() function : usage variations - different arrays(Bug#43424)
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_combine(array $keys, array $values)
  6. * Description: Creates an array by using the elements of the first parameter as keys
  7. * and the elements of the second as the corresponding values
  8. * Source code: ext/standard/array.c
  9. */
  10. /*
  11. * Passing different types of arrays to both $keys and $values arguments and testing whether
  12. * array_combine() behaves in an expected way with the arguments passed to the function
  13. */
  14. echo "*** Testing array_combine() : Passing different types of arrays to both \$keys and \$values argument ***\n";
  15. /* Different heredoc strings passed as argument to arrays */
  16. // heredoc with blank line
  17. $blank_line = <<<EOT
  18. EOT;
  19. // heredoc with multiline string
  20. $multiline_string = <<<EOT
  21. hello world
  22. The quick brown fox jumped over;
  23. the lazy dog
  24. This is a double quoted string
  25. EOT;
  26. // heredoc with different whitespaces
  27. $diff_whitespaces = <<<EOT
  28. hello\r world\t
  29. 1111\t\t != 2222\v\v
  30. heredoc\ndouble quoted string. with\vdifferent\fwhite\vspaces
  31. EOT;
  32. // heredoc with quoted strings and numeric values
  33. $numeric_string = <<<EOT
  34. 11 < 12. 123 >22
  35. 'single quoted string'
  36. "double quoted string"
  37. 2222 != 1111.\t 0000 = 0000\n
  38. EOT;
  39. // arrays passed to $keys argument
  40. $arrays = array (
  41. /*1*/ array(1, 2), // with default keys and numeric values
  42. array(1.1, 2.2), // with default keys & float values
  43. array(false,true), // with default keys and boolean values
  44. array(), // empty array
  45. /*5*/ array(NULL), // with NULL
  46. array("a\v\f","aaaa\r","b","b\tbbb","c","\[\]\!\@\#\$\%\^\&\*\(\)\{\}"), // with double quoted strings
  47. array('a\v\f','aaaa\r','b','b\tbbb','c','\[\]\!\@\#\$\%\^\&\*\(\)\{\}'), // with single quoted strings
  48. array("h1" => $blank_line, "h2" => $multiline_string, "h3" => $diff_whitespaces, $numeric_string), // with heredocs
  49. // associative arrays
  50. /*9*/ array(1 => "one", 2 => "two", 3 => "three"), // explicit numeric keys, string values
  51. array("one" => 1, "two" => 2, "three" => 3 ), // string keys & numeric values
  52. array( 1 => 10, 2 => 20, 4 => 40, 3 => 30), // explicit numeric keys and numeric values
  53. array( "one" => "ten", "two" => "twenty", "three" => "thirty"), // string key/value
  54. array("one" => 1, 2 => "two", 4 => "four"), //mixed
  55. // associative array, containing null/empty/boolean values as key/value
  56. /*14*/ array(NULL => "NULL", null => "null", "NULL" => NULL, "null" => null),
  57. array(true => "true", false => "false", "false" => false, "true" => true),
  58. array("" => "emptyd", '' => 'emptys', "emptyd" => "", 'emptys' => ''),
  59. array(1 => '', 2 => "", 3 => NULL, 4 => null, 5 => false, 6 => true),
  60. array('' => 1, "" => 2, NULL => 3, null => 4, false => 5, true => 6),
  61. // array with repetative keys
  62. /*19*/ array("One" => 1, "two" => 2, "One" => 10, "two" => 20, "three" => 3)
  63. );
  64. // loop through each sub-array within $arrays to check the behavior of array_combine()
  65. // same arrays are passed to both $keys and $values
  66. $iterator = 1;
  67. foreach($arrays as $array) {
  68. echo "-- Iteration $iterator --\n";
  69. var_dump( array_combine($array, $array) );
  70. $iterator++;
  71. }
  72. echo "Done";
  73. ?>
  74. --EXPECTF--
  75. *** Testing array_combine() : Passing different types of arrays to both $keys and $values argument ***
  76. -- Iteration 1 --
  77. array(2) {
  78. [1]=>
  79. int(1)
  80. [2]=>
  81. int(2)
  82. }
  83. -- Iteration 2 --
  84. array(2) {
  85. ["1.1"]=>
  86. float(1.1)
  87. ["2.2"]=>
  88. float(2.2)
  89. }
  90. -- Iteration 3 --
  91. array(2) {
  92. [""]=>
  93. bool(false)
  94. [1]=>
  95. bool(true)
  96. }
  97. -- Iteration 4 --
  98. array(0) {
  99. }
  100. -- Iteration 5 --
  101. array(1) {
  102. [""]=>
  103. NULL
  104. }
  105. -- Iteration 6 --
  106. array(6) {
  107. ["a "]=>
  108. string(3) "a "
  109. ["aaaa "]=>
  110. string(5) "aaaa "
  111. ["b"]=>
  112. string(1) "b"
  113. ["b bbb"]=>
  114. string(5) "b bbb"
  115. ["c"]=>
  116. string(1) "c"
  117. ["\[\]\!\@\#$\%\^\&\*\(\)\{\}"]=>
  118. string(27) "\[\]\!\@\#$\%\^\&\*\(\)\{\}"
  119. }
  120. -- Iteration 7 --
  121. array(6) {
  122. ["a\v\f"]=>
  123. string(5) "a\v\f"
  124. ["aaaa\r"]=>
  125. string(6) "aaaa\r"
  126. ["b"]=>
  127. string(1) "b"
  128. ["b\tbbb"]=>
  129. string(6) "b\tbbb"
  130. ["c"]=>
  131. string(1) "c"
  132. ["\[\]\!\@\#\$\%\^\&\*\(\)\{\}"]=>
  133. string(28) "\[\]\!\@\#\$\%\^\&\*\(\)\{\}"
  134. }
  135. -- Iteration 8 --
  136. array(4) {
  137. ["
  138. "]=>
  139. string(1) "
  140. "
  141. ["hello world
  142. The quick brown fox jumped over;
  143. the lazy dog
  144. This is a double quoted string"]=>
  145. string(88) "hello world
  146. The quick brown fox jumped over;
  147. the lazy dog
  148. This is a double quoted string"
  149. ["hello world
  150. 1111 != 2222
  151. heredoc
  152. double quoted string. with different white spaces"]=>
  153. string(88) "hello world
  154. 1111 != 2222
  155. heredoc
  156. double quoted string. with different white spaces"
  157. ["11 < 12. 123 >22
  158. 'single quoted string'
  159. "double quoted string"
  160. 2222 != 1111. 0000 = 0000
  161. "]=>
  162. string(90) "11 < 12. 123 >22
  163. 'single quoted string'
  164. "double quoted string"
  165. 2222 != 1111. 0000 = 0000
  166. "
  167. }
  168. -- Iteration 9 --
  169. array(3) {
  170. ["one"]=>
  171. string(3) "one"
  172. ["two"]=>
  173. string(3) "two"
  174. ["three"]=>
  175. string(5) "three"
  176. }
  177. -- Iteration 10 --
  178. array(3) {
  179. [1]=>
  180. int(1)
  181. [2]=>
  182. int(2)
  183. [3]=>
  184. int(3)
  185. }
  186. -- Iteration 11 --
  187. array(4) {
  188. [10]=>
  189. int(10)
  190. [20]=>
  191. int(20)
  192. [40]=>
  193. int(40)
  194. [30]=>
  195. int(30)
  196. }
  197. -- Iteration 12 --
  198. array(3) {
  199. ["ten"]=>
  200. string(3) "ten"
  201. ["twenty"]=>
  202. string(6) "twenty"
  203. ["thirty"]=>
  204. string(6) "thirty"
  205. }
  206. -- Iteration 13 --
  207. array(3) {
  208. [1]=>
  209. int(1)
  210. ["two"]=>
  211. string(3) "two"
  212. ["four"]=>
  213. string(4) "four"
  214. }
  215. -- Iteration 14 --
  216. array(2) {
  217. ["null"]=>
  218. string(4) "null"
  219. [""]=>
  220. NULL
  221. }
  222. -- Iteration 15 --
  223. array(4) {
  224. ["true"]=>
  225. string(4) "true"
  226. ["false"]=>
  227. string(5) "false"
  228. [""]=>
  229. bool(false)
  230. [1]=>
  231. bool(true)
  232. }
  233. -- Iteration 16 --
  234. array(2) {
  235. ["emptys"]=>
  236. string(6) "emptys"
  237. [""]=>
  238. string(0) ""
  239. }
  240. -- Iteration 17 --
  241. array(2) {
  242. [""]=>
  243. bool(false)
  244. [1]=>
  245. bool(true)
  246. }
  247. -- Iteration 18 --
  248. array(3) {
  249. [4]=>
  250. int(4)
  251. [5]=>
  252. int(5)
  253. [6]=>
  254. int(6)
  255. }
  256. -- Iteration 19 --
  257. array(3) {
  258. [10]=>
  259. int(10)
  260. [20]=>
  261. int(20)
  262. [3]=>
  263. int(3)
  264. }
  265. Done