array_unique_variation4.phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. --TEST--
  2. Test array_unique() function : usage variations - associative array with different values
  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 values to $input argument.
  12. */
  13. echo "*** Testing array_unique() : assoc. array with diff. values 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. // associative arrays with different values
  31. $inputs = array (
  32. // arrays with integer values
  33. /*1*/ array('0' => 0, '1' => 0),
  34. array("one" => 1, 'two' => 2, "three" => 1, 4 => 1),
  35. // arrays with float values
  36. /*3*/ array("float1" => 2.3333, "float2" => 2.3333),
  37. array("f1" => 1.2, 'f2' => 3.33, 3 => 4.89999922839999, 'f4' => 1.2),
  38. // arrays with string values
  39. /*5*/ array(111 => "\tHello", "red" => "col\tor", 2 => "\v\fworld", 3.3 => "\tHello"),
  40. array(111 => '\tHello', "red" => 'col\tor', 2 => '\v\fworld', 3.3 => '\tHello'),
  41. array(1 => "hello", "heredoc" => $heredoc, $heredoc),
  42. // array with object, unset variable and resource variable
  43. /*8*/ array(11 => new classA(), "unset" => @$unset_var, "resource" => $fp, new classA(), $fp),
  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. values to $input argument ***
  57. -- Iteration 1 --
  58. array(1) {
  59. [0]=>
  60. int(0)
  61. }
  62. -- Iteration 2 --
  63. array(2) {
  64. ["one"]=>
  65. int(1)
  66. ["two"]=>
  67. int(2)
  68. }
  69. -- Iteration 3 --
  70. array(1) {
  71. ["float1"]=>
  72. float(2.3333)
  73. }
  74. -- Iteration 4 --
  75. array(3) {
  76. ["f1"]=>
  77. float(1.2)
  78. ["f2"]=>
  79. float(3.33)
  80. [3]=>
  81. float(4.8999992284)
  82. }
  83. -- Iteration 5 --
  84. array(3) {
  85. [111]=>
  86. string(6) " Hello"
  87. ["red"]=>
  88. string(6) "col or"
  89. [2]=>
  90. string(7) " world"
  91. }
  92. -- Iteration 6 --
  93. array(3) {
  94. [111]=>
  95. string(7) "\tHello"
  96. ["red"]=>
  97. string(7) "col\tor"
  98. [2]=>
  99. string(9) "\v\fworld"
  100. }
  101. -- Iteration 7 --
  102. array(2) {
  103. [1]=>
  104. string(5) "hello"
  105. ["heredoc"]=>
  106. string(11) "Hello world"
  107. }
  108. -- Iteration 8 --
  109. array(3) {
  110. [11]=>
  111. object(classA)#%d (0) {
  112. }
  113. ["unset"]=>
  114. NULL
  115. ["resource"]=>
  116. resource(%d) of type (stream)
  117. }
  118. Done