008.phpt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. --TEST--
  2. Test array_intersect and array_intersect_assoc behaviour
  3. --FILE--
  4. <?php
  5. //-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=- TEST 1 -=-=-=-=-
  6. $a = array(1,"big"=>2,2,6,3,5,3,3,454,'some_string',3,3,3,3,3,3,3,3,17);
  7. $b = array(2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,17,25,'some_string',7,8,9,109,78,17);
  8. $c = array(-1,2,1,15,25,17);
  9. echo str_repeat("-=",10)." TEST 1 ".str_repeat("-=",20)."\n";
  10. echo '$a='.var_export($a,TRUE).";\n";
  11. echo '$b='.var_export($b,TRUE).";\n";
  12. echo '$c='.var_export($c,TRUE).";\n";
  13. echo 'array_intersect($a,$b,$c);'."\n";
  14. var_dump(array_intersect($a,$b,$c));
  15. echo 'array_intersect_assoc($a,$b,$c);'."\n";
  16. var_dump(array_intersect_assoc($a,$b,$c));
  17. echo 'array_intersect($a,$b);'."\n";
  18. var_dump(array_intersect($a,$b));
  19. echo 'array_intersect_assoc($a,$b);'."\n";
  20. var_dump(array_intersect_assoc($a,$b));
  21. //-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=- TEST 2 -=-=-=-=-=-
  22. $a = array(
  23. 'a'=>2,
  24. 'b'=>'some',
  25. 'c'=>'done',
  26. 'z'=>'foo',
  27. 'f'=>5,
  28. 'fan'=>'fen',
  29. 'bad'=>'bed',
  30. 'gate'=>'web',
  31. 7=>18,
  32. 9=>25,
  33. 11=>42,
  34. 12=>42,
  35. 45=>42,
  36. 73=>'foo',
  37. 95=>'some',
  38. 'som3'=>'some',
  39. 'want'=>'wanna');
  40. $b = array(
  41. 'a'=>7,
  42. 7=>18,
  43. 9=>13,
  44. 11=>42,
  45. 45=>46,
  46. 'som3'=>'some',
  47. 'foo'=>'some',
  48. 'goo'=>'foo',
  49. 'f'=>5,
  50. 'z'=>'equal',
  51. 'gate'=>'web'
  52. );
  53. $c = array(
  54. 'gate'=>'web',
  55. 73=>'foo',
  56. 95=>'some'
  57. );
  58. echo str_repeat("-=",10)." TEST 2 ".str_repeat("-=",20)."\n";
  59. echo '$a='.var_export($a,TRUE).";\n";
  60. echo '$b='.var_export($b,TRUE).";\n";
  61. echo '$c='.var_export($c,TRUE).";\n";
  62. echo "\n\nResults:\n\n";
  63. echo 'array_intersect($a,$b,$c);'."\n";
  64. var_dump(array_intersect($a,$b,$c));
  65. echo 'array_intersect_assoc($a,$b,$c);'."\n";
  66. var_dump(array_intersect_assoc($a,$b,$c));
  67. echo 'array_intersect($a,$b);'."\n";
  68. var_dump(array_intersect($a,$b));
  69. echo 'array_intersect_assoc($a,$b);'."\n";
  70. var_dump(array_intersect_assoc($a,$b));
  71. ?>
  72. --EXPECT--
  73. -=-=-=-=-=-=-=-=-=-= TEST 1 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  74. $a=array (
  75. 0 => 1,
  76. 'big' => 2,
  77. 1 => 2,
  78. 2 => 6,
  79. 3 => 3,
  80. 4 => 5,
  81. 5 => 3,
  82. 6 => 3,
  83. 7 => 454,
  84. 8 => 'some_string',
  85. 9 => 3,
  86. 10 => 3,
  87. 11 => 3,
  88. 12 => 3,
  89. 13 => 3,
  90. 14 => 3,
  91. 15 => 3,
  92. 16 => 3,
  93. 17 => 17,
  94. );
  95. $b=array (
  96. 0 => 2,
  97. 1 => 2,
  98. 2 => 3,
  99. 3 => 3,
  100. 4 => 3,
  101. 5 => 3,
  102. 6 => 3,
  103. 7 => 3,
  104. 8 => 3,
  105. 9 => 3,
  106. 10 => 3,
  107. 11 => 3,
  108. 12 => 3,
  109. 13 => 3,
  110. 14 => 3,
  111. 15 => 17,
  112. 16 => 25,
  113. 17 => 'some_string',
  114. 18 => 7,
  115. 19 => 8,
  116. 20 => 9,
  117. 21 => 109,
  118. 22 => 78,
  119. 23 => 17,
  120. );
  121. $c=array (
  122. 0 => -1,
  123. 1 => 2,
  124. 2 => 1,
  125. 3 => 15,
  126. 4 => 25,
  127. 5 => 17,
  128. );
  129. array_intersect($a,$b,$c);
  130. array(3) {
  131. ["big"]=>
  132. int(2)
  133. [1]=>
  134. int(2)
  135. [17]=>
  136. int(17)
  137. }
  138. array_intersect_assoc($a,$b,$c);
  139. array(1) {
  140. [1]=>
  141. int(2)
  142. }
  143. array_intersect($a,$b);
  144. array(15) {
  145. ["big"]=>
  146. int(2)
  147. [1]=>
  148. int(2)
  149. [3]=>
  150. int(3)
  151. [5]=>
  152. int(3)
  153. [6]=>
  154. int(3)
  155. [8]=>
  156. string(11) "some_string"
  157. [9]=>
  158. int(3)
  159. [10]=>
  160. int(3)
  161. [11]=>
  162. int(3)
  163. [12]=>
  164. int(3)
  165. [13]=>
  166. int(3)
  167. [14]=>
  168. int(3)
  169. [15]=>
  170. int(3)
  171. [16]=>
  172. int(3)
  173. [17]=>
  174. int(17)
  175. }
  176. array_intersect_assoc($a,$b);
  177. array(10) {
  178. [1]=>
  179. int(2)
  180. [3]=>
  181. int(3)
  182. [5]=>
  183. int(3)
  184. [6]=>
  185. int(3)
  186. [9]=>
  187. int(3)
  188. [10]=>
  189. int(3)
  190. [11]=>
  191. int(3)
  192. [12]=>
  193. int(3)
  194. [13]=>
  195. int(3)
  196. [14]=>
  197. int(3)
  198. }
  199. -=-=-=-=-=-=-=-=-=-= TEST 2 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  200. $a=array (
  201. 'a' => 2,
  202. 'b' => 'some',
  203. 'c' => 'done',
  204. 'z' => 'foo',
  205. 'f' => 5,
  206. 'fan' => 'fen',
  207. 'bad' => 'bed',
  208. 'gate' => 'web',
  209. 7 => 18,
  210. 9 => 25,
  211. 11 => 42,
  212. 12 => 42,
  213. 45 => 42,
  214. 73 => 'foo',
  215. 95 => 'some',
  216. 'som3' => 'some',
  217. 'want' => 'wanna',
  218. );
  219. $b=array (
  220. 'a' => 7,
  221. 7 => 18,
  222. 9 => 13,
  223. 11 => 42,
  224. 45 => 46,
  225. 'som3' => 'some',
  226. 'foo' => 'some',
  227. 'goo' => 'foo',
  228. 'f' => 5,
  229. 'z' => 'equal',
  230. 'gate' => 'web',
  231. );
  232. $c=array (
  233. 'gate' => 'web',
  234. 73 => 'foo',
  235. 95 => 'some',
  236. );
  237. Results:
  238. array_intersect($a,$b,$c);
  239. array(6) {
  240. ["b"]=>
  241. string(4) "some"
  242. ["z"]=>
  243. string(3) "foo"
  244. ["gate"]=>
  245. string(3) "web"
  246. [73]=>
  247. string(3) "foo"
  248. [95]=>
  249. string(4) "some"
  250. ["som3"]=>
  251. string(4) "some"
  252. }
  253. array_intersect_assoc($a,$b,$c);
  254. array(1) {
  255. ["gate"]=>
  256. string(3) "web"
  257. }
  258. array_intersect($a,$b);
  259. array(11) {
  260. ["b"]=>
  261. string(4) "some"
  262. ["z"]=>
  263. string(3) "foo"
  264. ["f"]=>
  265. int(5)
  266. ["gate"]=>
  267. string(3) "web"
  268. [7]=>
  269. int(18)
  270. [11]=>
  271. int(42)
  272. [12]=>
  273. int(42)
  274. [45]=>
  275. int(42)
  276. [73]=>
  277. string(3) "foo"
  278. [95]=>
  279. string(4) "some"
  280. ["som3"]=>
  281. string(4) "some"
  282. }
  283. array_intersect_assoc($a,$b);
  284. array(5) {
  285. ["f"]=>
  286. int(5)
  287. ["gate"]=>
  288. string(3) "web"
  289. [7]=>
  290. int(18)
  291. [11]=>
  292. int(42)
  293. ["som3"]=>
  294. string(4) "some"
  295. }