array_intersect_variation6.phpt 3.5 KB

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