array_unique_variation2.phpt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. --TEST--
  2. Test array_unique() function : usage variations - different arrays for 'input' argument
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_unique(array $input)
  6. * Description: Removes duplicate values from array
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Passing different arrays to $input argument and testing whether
  11. * array_unique() behaves in an expected way.
  12. */
  13. echo "*** Testing array_unique() : Passing different arrays to \$input argument ***\n";
  14. /* Different heredoc strings passed as argument to arrays */
  15. // heredoc with blank line
  16. $blank_line = <<<EOT
  17. EOT;
  18. // heredoc with multiline string
  19. $multiline_string = <<<EOT
  20. hello world
  21. The quick brown fox jumped over;
  22. the lazy dog
  23. This is a double quoted string
  24. EOT;
  25. // heredoc with different whitespaces
  26. $diff_whitespaces = <<<EOT
  27. hello\r world\t
  28. 1111\t\t != 2222\v\v
  29. heredoc\ndouble quoted string. with\vdifferent\fwhite\vspaces
  30. EOT;
  31. // heredoc with quoted strings and numeric values
  32. $numeric_string = <<<EOT
  33. 11 < 12. 123 >22
  34. 'single quoted string'
  35. "double quoted string"
  36. 2222 != 1111.\t 0000 = 0000\n
  37. EOT;
  38. // arrays passed to $input argument
  39. $inputs = array (
  40. /*1*/ array(1, 2, 2, 1), // with default keys and numeric values
  41. array(1.1, 2.2, 1.1), // with default keys & float values
  42. array(false, true, false), // with default keys and boolean values
  43. array(), // empty array
  44. /*5*/ array(NULL, null), // with NULL
  45. array("a\v\f", "aaaa\r", "b", "aaaa\r", "\[\]\!\@\#\$\%\^\&\*\(\)\{\}"), // with double quoted strings
  46. array('a\v\f', 'aaaa\r', 'b', 'aaaa\r', '\[\]\!\@\#\$\%\^\&\*\(\)\{\}'), // with single quoted strings
  47. array("h1" => $blank_line, "h2" => $multiline_string, "h3" => $diff_whitespaces, $blank_line), // with heredocs
  48. // associative arrays
  49. /*9*/ array(1 => "one", 2 => "two", 2 => "two"), // explicit numeric keys, string values
  50. array("one" => 1, "two" => 2, "1" => 1 ), // string keys & numeric values
  51. array( 1 => 10, 2 => 20, 4 => 40, 5 => 10), // explicit numeric keys and numeric values
  52. array( "one" => "ten", "two" => "twenty", "10" => "ten"), // string key/value
  53. array("one" => 1, 2 => "two", 4 => "four"), //mixed
  54. // associative array, containing null/empty/boolean values as key/value
  55. /*14*/ array(NULL => "NULL", null => "null", "NULL" => NULL, "null" => null),
  56. array(true => "true", false => "false", "false" => false, "true" => true),
  57. array("" => "emptyd", '' => 'emptys', "emptyd" => "", 'emptys' => ''),
  58. array(1 => '', 2 => "", 3 => NULL, 4 => null, 5 => false, 6 => true),
  59. /*18*/ array('' => 1, "" => 2, NULL => 3, null => 4, false => 5, true => 6),
  60. );
  61. // loop through each sub-array of $inputs to check the behavior of array_unique()
  62. $iterator = 1;
  63. foreach($inputs as $input) {
  64. echo "-- Iteration $iterator --\n";
  65. var_dump( array_unique($input, SORT_STRING) );
  66. $iterator++;
  67. }
  68. echo "Done";
  69. ?>
  70. --EXPECTF--
  71. *** Testing array_unique() : Passing different arrays to $input argument ***
  72. -- Iteration 1 --
  73. array(2) {
  74. [0]=>
  75. int(1)
  76. [1]=>
  77. int(2)
  78. }
  79. -- Iteration 2 --
  80. array(2) {
  81. [0]=>
  82. float(1.1)
  83. [1]=>
  84. float(2.2)
  85. }
  86. -- Iteration 3 --
  87. array(2) {
  88. [0]=>
  89. bool(false)
  90. [1]=>
  91. bool(true)
  92. }
  93. -- Iteration 4 --
  94. array(0) {
  95. }
  96. -- Iteration 5 --
  97. array(1) {
  98. [0]=>
  99. NULL
  100. }
  101. -- Iteration 6 --
  102. array(4) {
  103. [0]=>
  104. %unicode|string%(3) "a "
  105. [1]=>
  106. %unicode|string%(5) "aaaa "
  107. [2]=>
  108. %unicode|string%(1) "b"
  109. [4]=>
  110. %unicode|string%(27) "\[\]\!\@\#$\%\^\&\*\(\)\{\}"
  111. }
  112. -- Iteration 7 --
  113. array(4) {
  114. [0]=>
  115. %unicode|string%(5) "a\v\f"
  116. [1]=>
  117. %unicode|string%(6) "aaaa\r"
  118. [2]=>
  119. %unicode|string%(1) "b"
  120. [4]=>
  121. %unicode|string%(28) "\[\]\!\@\#\$\%\^\&\*\(\)\{\}"
  122. }
  123. -- Iteration 8 --
  124. array(3) {
  125. [%b|u%"h1"]=>
  126. %unicode|string%(1) "
  127. "
  128. [%b|u%"h2"]=>
  129. %unicode|string%(88) "hello world
  130. The quick brown fox jumped over;
  131. the lazy dog
  132. This is a double quoted string"
  133. [%b|u%"h3"]=>
  134. %unicode|string%(88) "hello world
  135. 1111 != 2222
  136. heredoc
  137. double quoted string. with different white spaces"
  138. }
  139. -- Iteration 9 --
  140. array(2) {
  141. [1]=>
  142. %unicode|string%(3) "one"
  143. [2]=>
  144. %unicode|string%(3) "two"
  145. }
  146. -- Iteration 10 --
  147. array(2) {
  148. [%b|u%"one"]=>
  149. int(1)
  150. [%b|u%"two"]=>
  151. int(2)
  152. }
  153. -- Iteration 11 --
  154. array(3) {
  155. [1]=>
  156. int(10)
  157. [2]=>
  158. int(20)
  159. [4]=>
  160. int(40)
  161. }
  162. -- Iteration 12 --
  163. array(2) {
  164. [%b|u%"one"]=>
  165. %unicode|string%(3) "ten"
  166. [%b|u%"two"]=>
  167. %unicode|string%(6) "twenty"
  168. }
  169. -- Iteration 13 --
  170. array(3) {
  171. [%b|u%"one"]=>
  172. int(1)
  173. [2]=>
  174. %unicode|string%(3) "two"
  175. [4]=>
  176. %unicode|string%(4) "four"
  177. }
  178. -- Iteration 14 --
  179. array(2) {
  180. [%b|u%""]=>
  181. %unicode|string%(4) "null"
  182. [%b|u%"NULL"]=>
  183. NULL
  184. }
  185. -- Iteration 15 --
  186. array(4) {
  187. [1]=>
  188. %unicode|string%(4) "true"
  189. [0]=>
  190. %unicode|string%(5) "false"
  191. [%b|u%"false"]=>
  192. bool(false)
  193. [%b|u%"true"]=>
  194. bool(true)
  195. }
  196. -- Iteration 16 --
  197. array(2) {
  198. [%b|u%""]=>
  199. %unicode|string%(6) "emptys"
  200. [%b|u%"emptyd"]=>
  201. %unicode|string%(0) ""
  202. }
  203. -- Iteration 17 --
  204. array(2) {
  205. [1]=>
  206. %unicode|string%(0) ""
  207. [6]=>
  208. bool(true)
  209. }
  210. -- Iteration 18 --
  211. array(3) {
  212. [%b|u%""]=>
  213. int(4)
  214. [0]=>
  215. int(5)
  216. [1]=>
  217. int(6)
  218. }
  219. Done