array_intersect_variation4.phpt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. --TEST--
  2. Test array_intersect() function : usage variations - different arrays for 'arr2' argument
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_intersect(array $arr1, array $arr2 [, array $...])
  6. * Description: Returns the entries of arr1 that have values which are present in all the other arguments
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Passing different types of arrays to $arr2 argument and testing whether
  11. * array_intersect() behaves in expected way with the other arguments passed to the function.
  12. * The $arr1 argument is a fixed array.
  13. */
  14. echo "*** Testing array_intersect() : Passing different types of arrays to \$arr2 argument ***\n";
  15. /* Different heredoc strings passed as argument to $arr2 */
  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 big 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. // array to be passsed to $arr1 argument
  40. $arr1 = array (
  41. 1, 1.1, "hello", "one", NULL, 2,
  42. 'world', true, false, false => 5, 'aaaa\r', "aaaa\r",
  43. $numeric_string, $diff_whitespaces,
  44. "one" => "ten", 4 => "four", "two" => 2, 2 => "two",
  45. '', null => "null", '' => 'emptys'
  46. );
  47. // arrays to be passed to $arr2 argument
  48. $arrays = array (
  49. /*1*/ array(1, 2), // array with default keys and numeric values
  50. array(1.1, 2.2), // array with default keys & float values
  51. array(false,true), // array with default keys and boolean values
  52. array(), // empty array
  53. /*5*/ array(NULL), // array with NULL
  54. array("a\v\f","aaaa\r","b","b\tbbb","c","\[\]\!\@\#\$\%\^\&\*\(\)\{\}"), // array with double quoted strings
  55. array('a\v\f','aaaa\r','b','b\tbbb','c','\[\]\!\@\#\$\%\^\&\*\(\)\{\}'), // array with single quoted strings
  56. array($blank_line, $multiline_string, $diff_whitespaces, $numeric_string), // array with heredocs
  57. // associative arrays
  58. /*9*/ array(1 => "one", 2 => "two", 3 => "three"), // explicit numeric keys, string values
  59. array("one" => 1, "two" => 2, "three" => 3 ), // string keys & numeric values
  60. array( 1 => 10, 2 => 20, 4 => 40, 3 => 30), // explicit numeric keys and numeric values
  61. array( "one" => "ten", "two" => "twenty", "three" => "thirty"), // string key/value
  62. array("one" => 1, 2 => "two", 4 => "four"), //mixed
  63. // associative array, containing null/empty/boolean values as key/value
  64. /*14*/ array(NULL => "NULL", null => "null", "NULL" => NULL, "null" => null),
  65. array(true => "true", false => "false", "false" => false, "true" => true),
  66. array("" => "emptyd", '' => 'emptys', "emptyd" => "", 'emptys' => ''),
  67. array(1 => '', 2 => "", 3 => NULL, 4 => null, 5 => false, 6 => true),
  68. array('' => 1, "" => 2, NULL => 3, null => 4, false => 5, true => 6),
  69. // array with repetative keys
  70. /*19*/ array("One" => 1, "two" => 2, "One" => 10, "two" => 20, "three" => 3)
  71. );
  72. // loop through each sub-array within $arrrays to check the behavior of array_intersect()
  73. $iterator = 1;
  74. foreach($arrays as $arr2) {
  75. echo "-- Iteration $iterator --\n";
  76. // Calling array_intersect() with default arguments
  77. var_dump( array_intersect($arr1, $arr2) );
  78. // Calling array_intersect() with more arguments
  79. // additional argument passed is the same as $arr1 argument
  80. var_dump( array_intersect($arr1, $arr2, $arr1) );
  81. $iterator++;
  82. }
  83. echo "Done";
  84. ?>
  85. --EXPECTF--
  86. *** Testing array_intersect() : Passing different types of arrays to $arr2 argument ***
  87. -- Iteration 1 --
  88. array(3) {
  89. [5]=>
  90. int(2)
  91. [7]=>
  92. bool(true)
  93. ["two"]=>
  94. int(2)
  95. }
  96. array(3) {
  97. [5]=>
  98. int(2)
  99. [7]=>
  100. bool(true)
  101. ["two"]=>
  102. int(2)
  103. }
  104. -- Iteration 2 --
  105. array(1) {
  106. [1]=>
  107. float(1.1)
  108. }
  109. array(1) {
  110. [1]=>
  111. float(1.1)
  112. }
  113. -- Iteration 3 --
  114. array(3) {
  115. [7]=>
  116. bool(true)
  117. [8]=>
  118. bool(false)
  119. [13]=>
  120. string(0) ""
  121. }
  122. array(3) {
  123. [7]=>
  124. bool(true)
  125. [8]=>
  126. bool(false)
  127. [13]=>
  128. string(0) ""
  129. }
  130. -- Iteration 4 --
  131. array(0) {
  132. }
  133. array(0) {
  134. }
  135. -- Iteration 5 --
  136. array(2) {
  137. [8]=>
  138. bool(false)
  139. [13]=>
  140. string(0) ""
  141. }
  142. array(2) {
  143. [8]=>
  144. bool(false)
  145. [13]=>
  146. string(0) ""
  147. }
  148. -- Iteration 6 --
  149. array(1) {
  150. [10]=>
  151. string(5) "aaaa "
  152. }
  153. array(1) {
  154. [10]=>
  155. string(5) "aaaa "
  156. }
  157. -- Iteration 7 --
  158. array(1) {
  159. [9]=>
  160. string(6) "aaaa\r"
  161. }
  162. array(1) {
  163. [9]=>
  164. string(6) "aaaa\r"
  165. }
  166. -- Iteration 8 --
  167. array(2) {
  168. [11]=>
  169. string(90) "11 < 12. 123 >22
  170. 'single quoted string'
  171. "double quoted string"
  172. 2222 != 1111. 0000 = 0000
  173. "
  174. [12]=>
  175. string(88) "hello world
  176. 1111 != 2222
  177. heredoc
  178. double quoted string. with different white spaces"
  179. }
  180. array(2) {
  181. [11]=>
  182. string(90) "11 < 12. 123 >22
  183. 'single quoted string'
  184. "double quoted string"
  185. 2222 != 1111. 0000 = 0000
  186. "
  187. [12]=>
  188. string(88) "hello world
  189. 1111 != 2222
  190. heredoc
  191. double quoted string. with different white spaces"
  192. }
  193. -- Iteration 9 --
  194. array(2) {
  195. [2]=>
  196. string(3) "two"
  197. [3]=>
  198. string(3) "one"
  199. }
  200. array(2) {
  201. [2]=>
  202. string(3) "two"
  203. [3]=>
  204. string(3) "one"
  205. }
  206. -- Iteration 10 --
  207. array(3) {
  208. [5]=>
  209. int(2)
  210. [7]=>
  211. bool(true)
  212. ["two"]=>
  213. int(2)
  214. }
  215. array(3) {
  216. [5]=>
  217. int(2)
  218. [7]=>
  219. bool(true)
  220. ["two"]=>
  221. int(2)
  222. }
  223. -- Iteration 11 --
  224. array(0) {
  225. }
  226. array(0) {
  227. }
  228. -- Iteration 12 --
  229. array(1) {
  230. ["one"]=>
  231. string(3) "ten"
  232. }
  233. array(1) {
  234. ["one"]=>
  235. string(3) "ten"
  236. }
  237. -- Iteration 13 --
  238. array(3) {
  239. [2]=>
  240. string(3) "two"
  241. [4]=>
  242. string(4) "four"
  243. [7]=>
  244. bool(true)
  245. }
  246. array(3) {
  247. [2]=>
  248. string(3) "two"
  249. [4]=>
  250. string(4) "four"
  251. [7]=>
  252. bool(true)
  253. }
  254. -- Iteration 14 --
  255. array(2) {
  256. [8]=>
  257. bool(false)
  258. [13]=>
  259. string(0) ""
  260. }
  261. array(2) {
  262. [8]=>
  263. bool(false)
  264. [13]=>
  265. string(0) ""
  266. }
  267. -- Iteration 15 --
  268. array(3) {
  269. [7]=>
  270. bool(true)
  271. [8]=>
  272. bool(false)
  273. [13]=>
  274. string(0) ""
  275. }
  276. array(3) {
  277. [7]=>
  278. bool(true)
  279. [8]=>
  280. bool(false)
  281. [13]=>
  282. string(0) ""
  283. }
  284. -- Iteration 16 --
  285. array(3) {
  286. [8]=>
  287. bool(false)
  288. [13]=>
  289. string(0) ""
  290. [""]=>
  291. string(6) "emptys"
  292. }
  293. array(3) {
  294. [8]=>
  295. bool(false)
  296. [13]=>
  297. string(0) ""
  298. [""]=>
  299. string(6) "emptys"
  300. }
  301. -- Iteration 17 --
  302. array(3) {
  303. [7]=>
  304. bool(true)
  305. [8]=>
  306. bool(false)
  307. [13]=>
  308. string(0) ""
  309. }
  310. array(3) {
  311. [7]=>
  312. bool(true)
  313. [8]=>
  314. bool(false)
  315. [13]=>
  316. string(0) ""
  317. }
  318. -- Iteration 18 --
  319. array(1) {
  320. [0]=>
  321. int(5)
  322. }
  323. array(1) {
  324. [0]=>
  325. int(5)
  326. }
  327. -- Iteration 19 --
  328. array(0) {
  329. }
  330. array(0) {
  331. }
  332. Done