array_unique_variation3.phpt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. --TEST--
  2. Test array_unique() function : usage variations - associative array with different keys
  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. * Testing the functionality of array_unique() by passing different
  11. * associative arrays having different keys to $input argument.
  12. */
  13. echo "*** Testing array_unique() : assoc. array with diff. keys passed to \$input argument ***\n";
  14. // get an unset variable
  15. $unset_var = 10;
  16. unset ($unset_var);
  17. // get a resource variable
  18. $fp = fopen(__FILE__, "r");
  19. // get a class
  20. class classA
  21. {
  22. public function __toString(){
  23. return "Class A object";
  24. }
  25. }
  26. // get a heredoc string
  27. $heredoc = <<<EOT
  28. Hello world
  29. EOT;
  30. // different associative arrays to be passed to $input argument
  31. $inputs = array (
  32. /*1*/ // arrays with integer keys
  33. array(0 => "0", 1 => "0"),
  34. array(1 => "1", 2 => "2", 3 => 1, 4 => "4"),
  35. // arrays with float keys
  36. /*3*/ array(2.3333 => "float", 44.44 => "float"),
  37. array(1.2 => "f1", 3.33 => "f2", 4.89999922839999 => "f1", 3333333.333333 => "f4"),
  38. // arrays with string keys
  39. /*5*/ array('\tHello' => 111, 're\td' => "color", '\v\fworld' => 2.2, 'pen\n' => 111),
  40. array("\tHello" => 111, "re\td" => "color", "\v\fworld" => 2.2, "pen\n" => 111),
  41. array("hello", $heredoc => "string", "string"),
  42. // array with object, unset variable and resource variable
  43. /*8*/ array(new classA() => 11, @$unset_var => "hello", $fp => 'resource', 11, "hello"),
  44. );
  45. // loop through each sub-array of $inputs to check the behavior of array_unique()
  46. $iterator = 1;
  47. foreach($inputs as $input) {
  48. echo "-- Iteration $iterator --\n";
  49. var_dump( array_unique($input) );
  50. $iterator++;
  51. }
  52. fclose($fp);
  53. echo "Done";
  54. ?>
  55. --EXPECTF--
  56. *** Testing array_unique() : assoc. array with diff. keys passed to $input argument ***
  57. Warning: Illegal offset type in %s on line %d
  58. Warning: Illegal offset type in %s on line %d
  59. -- Iteration 1 --
  60. array(1) {
  61. [0]=>
  62. string(1) "0"
  63. }
  64. -- Iteration 2 --
  65. array(3) {
  66. [1]=>
  67. string(1) "1"
  68. [2]=>
  69. string(1) "2"
  70. [4]=>
  71. string(1) "4"
  72. }
  73. -- Iteration 3 --
  74. array(1) {
  75. [2]=>
  76. string(5) "float"
  77. }
  78. -- Iteration 4 --
  79. array(3) {
  80. [1]=>
  81. string(2) "f1"
  82. [3]=>
  83. string(2) "f2"
  84. [3333333]=>
  85. string(2) "f4"
  86. }
  87. -- Iteration 5 --
  88. array(3) {
  89. ["\tHello"]=>
  90. int(111)
  91. ["re\td"]=>
  92. string(5) "color"
  93. ["\v\fworld"]=>
  94. float(2.2)
  95. }
  96. -- Iteration 6 --
  97. array(3) {
  98. [" Hello"]=>
  99. int(111)
  100. ["re d"]=>
  101. string(5) "color"
  102. [" world"]=>
  103. float(2.2)
  104. }
  105. -- Iteration 7 --
  106. array(2) {
  107. [0]=>
  108. string(5) "hello"
  109. ["Hello world"]=>
  110. string(6) "string"
  111. }
  112. -- Iteration 8 --
  113. array(2) {
  114. [""]=>
  115. string(5) "hello"
  116. [0]=>
  117. int(11)
  118. }
  119. Done