1--TEST--
2Test 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
12/* Test in_array() with haystack as sub-array and object */
13
14/* checking for sub-arrays with in_array() */
15echo "*** Testing sub-arrays with in_array() ***\n";
16$sub_array = array (
17  "one",
18  array(1, 2 => "two", "three" => 3),
19  4 => "four",
20  "five" => 5,
21  array('', 'i')
22);
23var_dump( in_array("four", $sub_array) );
24//checking for element in a sub-array
25var_dump( in_array(3, $sub_array[1]) );
26var_dump( in_array(array('','i'), $sub_array) );
27
28/* checking for objects in in_array() */
29echo "\n*** Testing objects with in_array() ***\n";
30class in_array_check {
31  public $array_var = array(1=>"one", "two"=>2, 3=>3);
32  public function foo() {
33    echo "Public function\n";
34  }
35}
36
37$in_array_obj = new in_array_check();  //creating new object
38//error: as wrong datatype for second argument
39var_dump( in_array("array_var", $in_array_obj) );
40//error: as wrong datatype for second argument
41var_dump( in_array("foo", $in_array_obj) );
42//element found as "one" exists in array $array_var
43var_dump( in_array("one", $in_array_obj->array_var) );
44
45echo "Done\n";
46?>
47--EXPECTF--
48*** Testing sub-arrays with in_array() ***
49bool(true)
50bool(true)
51bool(true)
52
53*** Testing objects with in_array() ***
54
55Warning: in_array() expects parameter 2 to be array, object given in %s on line %d
56NULL
57
58Warning: in_array() expects parameter 2 to be array, object given in %s on line %d
59NULL
60bool(true)
61Done
62