PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/zend/good/ext/standard/tests/array/array_diff_variation8.php

http://github.com/facebook/hiphop-php
PHP | 67 lines | 36 code | 13 blank | 18 comment | 0 complexity | 54ffc9e0b5e34caf32bd55f7d6f2e34f MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh
  2. /* Prototype : array array_diff(array $arr1, array $arr2 [, array ...])
  3. * Description: Returns the entries of $arr1 that have values which are
  4. * not present in any of the others arguments.
  5. * Source code: ext/standard/array.c
  6. */
  7. /*
  8. * Test array_diff() with associative arrays containing different data types as values
  9. */
  10. // get a class
  11. class classA
  12. {
  13. public function __toString() {
  14. return "Class A object";
  15. }
  16. }
  17. <<__EntryPoint>> function main(): void {
  18. echo "*** Testing array_diff() : usage variations ***\n";
  19. $array = darray['a' => '1', 'b' => '2', 'c' => '3'];
  20. // get an unset variable
  21. $unset_var = 10;
  22. unset ($unset_var);
  23. // get a resource variable
  24. $fp = fopen(__FILE__, "r");
  25. // get a heredoc string
  26. $heredoc = <<<EOT
  27. Hello world
  28. EOT;
  29. // associative arrays with different values
  30. $inputs = varray [
  31. // arrays with integer values
  32. /*1*/ darray['0' => 0, '1' => 0],
  33. darray["one" => 1, 'two' => 2, "three" => 1, 4 => 1],
  34. // arrays with float values
  35. /*3*/ darray["float1" => 2.3333, "float2" => 2.3333],
  36. darray["f1" => 1.2, 'f2' => 3.33, 3 => 4.89999922839999, 'f4' => 1.2],
  37. // arrays with string values
  38. /*5*/ darray[111 => "\tHello", "red" => "col\tor", 2 => "\v\fworld", 3.3 => "\tHello"],
  39. darray[111 => '\tHello', "red" => 'col\tor', 2 => '\v\fworld', 3.3 => '\tHello'],
  40. darray[1 => "hello", "heredoc" => $heredoc, 2 => $heredoc],
  41. // array with object, unset variable and resource variable
  42. /*8*/ darray[11 => new classA(), "unset" => @$unset_var, "resource" => $fp, 12 => new classA(), 13 => $fp],
  43. ];
  44. // loop through each sub-array of $inputs to check the behavior of array_unique()
  45. $iterator = 1;
  46. foreach($inputs as $input) {
  47. echo "-- Iteration $iterator --\n";
  48. var_dump( array_diff($array, $input) );
  49. var_dump( array_diff($input, $array) );
  50. $iterator++;
  51. }
  52. fclose($fp);
  53. echo "Done";
  54. }