array_intersect_variation8.phpt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. --TEST--
  2. Test array_intersect() function : usage variations - assoc array with diff values 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. * Testing the functionality of array_intersect() by passing different
  11. * associative arrays having different possible values to $arr2 argument.
  12. * The $arr1 argument is a fixed array.
  13. */
  14. echo "*** Testing array_intersect() : assoc array with diff values to \$arr2 argument ***\n";
  15. // get an unset variable
  16. $unset_var = 10;
  17. unset ($unset_var);
  18. // get a resource variable
  19. $fp = fopen(__FILE__, "r");
  20. // get a class
  21. class classA
  22. {
  23. public function __toString(){
  24. return "Class A object";
  25. }
  26. }
  27. // get a heredoc string
  28. $heredoc = <<<EOT
  29. Hello world
  30. EOT;
  31. // different variations of associative arrays to be passed to $arr2 argument
  32. $arrays = array (
  33. // empty array
  34. /*1*/ array(),
  35. // arrays with integer values
  36. array('0' => 0),
  37. array("1" => 1),
  38. array("one" => 1, 'two' => 2, "three" => 3, 4 => 4),
  39. // arrays with float values
  40. /*5*/ array("float" => 2.3333),
  41. array("f1" => 1.2, 'f2' => 3.33, 3 => 4.89999922839999, 'f4' => 33333333.333333),
  42. // arrays with string values
  43. /*7*/ array(111 => "\tHello", "red" => "col\tor", 2 => "\v\fworld", 3.3 => "pen\n"),
  44. array(111 => '\tHello', "red" => 'col\tor', 2 => '\v\fworld', 3.3 => 'pen\n'),
  45. array(1 => "hello", "heredoc" => $heredoc),
  46. // array with object, unset variable and resource variable
  47. /*10*/ array(11 => new classA(), "unset" => @$unset_var, "resource" => $fp),
  48. // array with mixed values
  49. /*11*/ array(1 => 'hello', 2 => new classA(), 222 => "fruit",
  50. 'resource' => $fp, "int" => 133, "float" => 444.432,
  51. "unset" => @$unset_var, "heredoc" => $heredoc)
  52. );
  53. // array to be passsed to $arr1 argument
  54. $arr1 = array(1, 2, 1.2, 2.3333, "col\tor", '\v\fworld', $fp,
  55. "Hello world", $heredoc, new classA(), 444.432, "fruit");
  56. // loop through each sub-array within $arrrays to check the behavior of array_intersect()
  57. $iterator = 1;
  58. foreach($arrays as $arr2) {
  59. echo "-- Iterator $iterator --\n";
  60. // Calling array_intersect() with default arguments
  61. var_dump( array_intersect($arr1, $arr2) );
  62. // Calling array_intersect() with more arguments.
  63. // additional argument passed is the same as $arr1 argument
  64. var_dump( array_intersect($arr1, $arr2, $arr1) );
  65. $iterator++;
  66. }
  67. // close the file resource used
  68. fclose($fp);
  69. echo "Done";
  70. ?>
  71. --EXPECTF--
  72. *** Testing array_intersect() : assoc array with diff values to $arr2 argument ***
  73. -- Iterator 1 --
  74. array(0) {
  75. }
  76. array(0) {
  77. }
  78. -- Iterator 2 --
  79. array(0) {
  80. }
  81. array(0) {
  82. }
  83. -- Iterator 3 --
  84. array(1) {
  85. [0]=>
  86. int(1)
  87. }
  88. array(1) {
  89. [0]=>
  90. int(1)
  91. }
  92. -- Iterator 4 --
  93. array(2) {
  94. [0]=>
  95. int(1)
  96. [1]=>
  97. int(2)
  98. }
  99. array(2) {
  100. [0]=>
  101. int(1)
  102. [1]=>
  103. int(2)
  104. }
  105. -- Iterator 5 --
  106. array(1) {
  107. [3]=>
  108. float(2.3333)
  109. }
  110. array(1) {
  111. [3]=>
  112. float(2.3333)
  113. }
  114. -- Iterator 6 --
  115. array(1) {
  116. [2]=>
  117. float(1.2)
  118. }
  119. array(1) {
  120. [2]=>
  121. float(1.2)
  122. }
  123. -- Iterator 7 --
  124. array(1) {
  125. [4]=>
  126. string(6) "col or"
  127. }
  128. array(1) {
  129. [4]=>
  130. string(6) "col or"
  131. }
  132. -- Iterator 8 --
  133. array(1) {
  134. [5]=>
  135. string(9) "\v\fworld"
  136. }
  137. array(1) {
  138. [5]=>
  139. string(9) "\v\fworld"
  140. }
  141. -- Iterator 9 --
  142. array(2) {
  143. [7]=>
  144. string(11) "Hello world"
  145. [8]=>
  146. string(11) "Hello world"
  147. }
  148. array(2) {
  149. [7]=>
  150. string(11) "Hello world"
  151. [8]=>
  152. string(11) "Hello world"
  153. }
  154. -- Iterator 10 --
  155. array(2) {
  156. [6]=>
  157. resource(%d) of type (stream)
  158. [9]=>
  159. object(classA)#%d (0) {
  160. }
  161. }
  162. array(2) {
  163. [6]=>
  164. resource(%d) of type (stream)
  165. [9]=>
  166. object(classA)#%d (0) {
  167. }
  168. }
  169. -- Iterator 11 --
  170. array(6) {
  171. [6]=>
  172. resource(%d) of type (stream)
  173. [7]=>
  174. string(11) "Hello world"
  175. [8]=>
  176. string(11) "Hello world"
  177. [9]=>
  178. object(classA)#%d (0) {
  179. }
  180. [10]=>
  181. float(444.432)
  182. [11]=>
  183. string(5) "fruit"
  184. }
  185. array(6) {
  186. [6]=>
  187. resource(%d) of type (stream)
  188. [7]=>
  189. string(11) "Hello world"
  190. [8]=>
  191. string(11) "Hello world"
  192. [9]=>
  193. object(classA)#%d (0) {
  194. }
  195. [10]=>
  196. float(444.432)
  197. [11]=>
  198. string(5) "fruit"
  199. }
  200. Done