array_diff_basic.phpt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. --TEST--
  2. Test array_diff() function : basic functionality
  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 not present
  7. * in any of the others arguments.
  8. * Source code: ext/standard/array.c
  9. */
  10. /*
  11. * Test basic functionality of array_diff
  12. */
  13. echo "*** Testing array_diff() : basic functionality ***\n";
  14. //Test indexed array with integers as elements
  15. $array_int1 = array (1, 2, 3, 4);
  16. $array_int2 = array (3, 4, 5, 6);
  17. echo "-- Test indexed array with integers as elements --\n";
  18. var_dump(array_diff($array_int1, $array_int2));
  19. var_dump(array_diff($array_int2, $array_int1));
  20. //Test indexed array with strings as elements
  21. $array_string1 = array ('one', 'two', 'three', 'four');
  22. $array_string2 = array ('three', 'four', 'five', 'six');
  23. echo "-- Test indexed array with strings as elements --\n";
  24. var_dump(array_diff($array_string1, $array_string2));
  25. var_dump(array_diff($array_string2, $array_string1));
  26. //Test associative array with strings as keys and integers as elements
  27. $array_assoc_int1 = array ('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4);
  28. $array_assoc_int2 = array ('three' => 3, 'four' => 4, 'five' => 5, 'six' => 6);
  29. echo "-- Test associative array with strings as keys and integers as elements --\n";
  30. var_dump(array_diff($array_assoc_int1, $array_assoc_int2));
  31. var_dump(array_diff($array_assoc_int2, $array_assoc_int1));
  32. //Test associative array with strings as keys and elements
  33. $array_assoc_str1 = array ('one' => 'un', 'two' => 'deux', 'three' => 'trois', 'four' => 'quatre');
  34. $array_assoc_str2 = array ('three' => 'trois', 'four' => 'quatre', 'five' => 'cinq', 'six' => 'six');
  35. echo "-- Test associative array with strings as keys and integers as elements --\n";
  36. var_dump(array_diff($array_assoc_str1, $array_assoc_str2));
  37. var_dump(array_diff($array_assoc_str2, $array_assoc_str1));
  38. echo "-- Test array_diff with more than 2 arguments --\n";
  39. var_dump(array_diff($array_int1, $array_int2, $array_string1, $array_string2));
  40. echo "Done";
  41. ?>
  42. --EXPECTF--
  43. *** Testing array_diff() : basic functionality ***
  44. -- Test indexed array with integers as elements --
  45. array(2) {
  46. [0]=>
  47. int(1)
  48. [1]=>
  49. int(2)
  50. }
  51. array(2) {
  52. [2]=>
  53. int(5)
  54. [3]=>
  55. int(6)
  56. }
  57. -- Test indexed array with strings as elements --
  58. array(2) {
  59. [0]=>
  60. string(3) "one"
  61. [1]=>
  62. string(3) "two"
  63. }
  64. array(2) {
  65. [2]=>
  66. string(4) "five"
  67. [3]=>
  68. string(3) "six"
  69. }
  70. -- Test associative array with strings as keys and integers as elements --
  71. array(2) {
  72. ["one"]=>
  73. int(1)
  74. ["two"]=>
  75. int(2)
  76. }
  77. array(2) {
  78. ["five"]=>
  79. int(5)
  80. ["six"]=>
  81. int(6)
  82. }
  83. -- Test associative array with strings as keys and integers as elements --
  84. array(2) {
  85. ["one"]=>
  86. string(2) "un"
  87. ["two"]=>
  88. string(4) "deux"
  89. }
  90. array(2) {
  91. ["five"]=>
  92. string(4) "cinq"
  93. ["six"]=>
  94. string(3) "six"
  95. }
  96. -- Test array_diff with more than 2 arguments --
  97. array(2) {
  98. [0]=>
  99. int(1)
  100. [1]=>
  101. int(2)
  102. }
  103. Done