array_diff_variation8.phpt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. --TEST--
  2. Test array_diff() function : usage variations - associative arrays contianing different data types
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_diff(array $arr1, array $arr2 [, array ...])
  6. * Description: Returns the entries of $arr1 that have values which are
  7. * not present in any of the others arguments.
  8. * Source code: ext/standard/array.c
  9. */
  10. /*
  11. * Test array_diff() with associative arrays containing different data types as values
  12. */
  13. echo "*** Testing array_diff() : usage variations ***\n";
  14. $array = array('a' => '1', 'b' => '2', 'c' => '3');
  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. // associative arrays with different values
  32. $inputs = array (
  33. // arrays with integer values
  34. /*1*/ array('0' => 0, '1' => 0),
  35. array("one" => 1, 'two' => 2, "three" => 1, 4 => 1),
  36. // arrays with float values
  37. /*3*/ array("float1" => 2.3333, "float2" => 2.3333),
  38. array("f1" => 1.2, 'f2' => 3.33, 3 => 4.89999922839999, 'f4' => 1.2),
  39. // arrays with string values
  40. /*5*/ array(111 => "\tHello", "red" => "col\tor", 2 => "\v\fworld", 3.3 => "\tHello"),
  41. array(111 => '\tHello', "red" => 'col\tor', 2 => '\v\fworld', 3.3 => '\tHello'),
  42. array(1 => "hello", "heredoc" => $heredoc, $heredoc),
  43. // array with object, unset variable and resource variable
  44. /*8*/ array(11 => new classA(), "unset" => @$unset_var, "resource" => $fp, new classA(), $fp),
  45. );
  46. // loop through each sub-array of $inputs to check the behavior of array_unique()
  47. $iterator = 1;
  48. foreach($inputs as $input) {
  49. echo "-- Iteration $iterator --\n";
  50. var_dump( array_diff($array, $input) );
  51. var_dump( array_diff($input, $array) );
  52. $iterator++;
  53. }
  54. fclose($fp);
  55. echo "Done";
  56. ?>
  57. --EXPECTF--
  58. *** Testing array_diff() : usage variations ***
  59. -- Iteration 1 --
  60. array(3) {
  61. ["a"]=>
  62. string(1) "1"
  63. ["b"]=>
  64. string(1) "2"
  65. ["c"]=>
  66. string(1) "3"
  67. }
  68. array(2) {
  69. [0]=>
  70. int(0)
  71. [1]=>
  72. int(0)
  73. }
  74. -- Iteration 2 --
  75. array(1) {
  76. ["c"]=>
  77. string(1) "3"
  78. }
  79. array(0) {
  80. }
  81. -- Iteration 3 --
  82. array(3) {
  83. ["a"]=>
  84. string(1) "1"
  85. ["b"]=>
  86. string(1) "2"
  87. ["c"]=>
  88. string(1) "3"
  89. }
  90. array(2) {
  91. ["float1"]=>
  92. float(2.3333)
  93. ["float2"]=>
  94. float(2.3333)
  95. }
  96. -- Iteration 4 --
  97. array(3) {
  98. ["a"]=>
  99. string(1) "1"
  100. ["b"]=>
  101. string(1) "2"
  102. ["c"]=>
  103. string(1) "3"
  104. }
  105. array(4) {
  106. ["f1"]=>
  107. float(1.2)
  108. ["f2"]=>
  109. float(3.33)
  110. [3]=>
  111. float(4.8999992284)
  112. ["f4"]=>
  113. float(1.2)
  114. }
  115. -- Iteration 5 --
  116. array(3) {
  117. ["a"]=>
  118. string(1) "1"
  119. ["b"]=>
  120. string(1) "2"
  121. ["c"]=>
  122. string(1) "3"
  123. }
  124. array(4) {
  125. [111]=>
  126. string(6) " Hello"
  127. ["red"]=>
  128. string(6) "col or"
  129. [2]=>
  130. string(7) " world"
  131. [3]=>
  132. string(6) " Hello"
  133. }
  134. -- Iteration 6 --
  135. array(3) {
  136. ["a"]=>
  137. string(1) "1"
  138. ["b"]=>
  139. string(1) "2"
  140. ["c"]=>
  141. string(1) "3"
  142. }
  143. array(4) {
  144. [111]=>
  145. string(7) "\tHello"
  146. ["red"]=>
  147. string(7) "col\tor"
  148. [2]=>
  149. string(9) "\v\fworld"
  150. [3]=>
  151. string(7) "\tHello"
  152. }
  153. -- Iteration 7 --
  154. array(3) {
  155. ["a"]=>
  156. string(1) "1"
  157. ["b"]=>
  158. string(1) "2"
  159. ["c"]=>
  160. string(1) "3"
  161. }
  162. array(3) {
  163. [1]=>
  164. string(5) "hello"
  165. ["heredoc"]=>
  166. string(11) "Hello world"
  167. [2]=>
  168. string(11) "Hello world"
  169. }
  170. -- Iteration 8 --
  171. array(3) {
  172. ["a"]=>
  173. string(1) "1"
  174. ["b"]=>
  175. string(1) "2"
  176. ["c"]=>
  177. string(1) "3"
  178. }
  179. array(5) {
  180. [11]=>
  181. object(classA)#%d (0) {
  182. }
  183. ["unset"]=>
  184. NULL
  185. ["resource"]=>
  186. resource(%d) of type (stream)
  187. [12]=>
  188. object(classA)#%d (0) {
  189. }
  190. [13]=>
  191. resource(%d) of type (stream)
  192. }
  193. Done