in_array_variation3.phpt 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. --TEST--
  2. Test in_array() function : usage variations - haystack as sub-array/object
  3. --FILE--
  4. <?php
  5. /*
  6. * Prototype : bool in_array ( mixed $needle, array $haystack [, bool $strict] )
  7. * Description: Searches haystack for needle and returns TRUE
  8. * if it is found in the array, FALSE otherwise.
  9. * Source Code: ext/standard/array.c
  10. */
  11. /* Test in_array() with haystack as sub-array and object */
  12. /* checking for sub-arrays with in_array() */
  13. echo "*** Testing sub-arrays with in_array() ***\n";
  14. $sub_array = array (
  15. "one",
  16. array(1, 2 => "two", "three" => 3),
  17. 4 => "four",
  18. "five" => 5,
  19. array('', 'i')
  20. );
  21. var_dump( in_array("four", $sub_array) );
  22. //checking for element in a sub-array
  23. var_dump( in_array(3, $sub_array[1]) );
  24. var_dump( in_array(array('','i'), $sub_array) );
  25. /* checking for objects in in_array() */
  26. echo "\n*** Testing objects with in_array() ***\n";
  27. class in_array_check {
  28. public $array_var = array(1=>"one", "two"=>2, 3=>3);
  29. public function foo() {
  30. echo "Public function\n";
  31. }
  32. }
  33. $in_array_obj = new in_array_check(); //creating new object
  34. //error: as wrong datatype for second argument
  35. var_dump( in_array("array_var", $in_array_obj) );
  36. //error: as wrong datatype for second argument
  37. var_dump( in_array("foo", $in_array_obj) );
  38. //element found as "one" exists in array $array_var
  39. var_dump( in_array("one", $in_array_obj->array_var) );
  40. echo "Done\n";
  41. ?>
  42. --EXPECTF--
  43. *** Testing sub-arrays with in_array() ***
  44. bool(true)
  45. bool(true)
  46. bool(true)
  47. *** Testing objects with in_array() ***
  48. Warning: in_array() expects parameter 2 to be array, object given in %s on line %d
  49. NULL
  50. Warning: in_array() expects parameter 2 to be array, object given in %s on line %d
  51. NULL
  52. bool(true)
  53. Done