Annotation of embedaddon/php/ext/standard/tests/array/in_array_variation3.phpt, revision 1.1.1.1

1.1       misho       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: 
                     12: /* Test in_array() with haystack as sub-array and object */
                     13: 
                     14: /* checking for sub-arrays with in_array() */
                     15: echo "*** 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: );
                     23: var_dump( in_array("four", $sub_array) );
                     24: //checking for element in a sub-array
                     25: var_dump( in_array(3, $sub_array[1]) ); 
                     26: var_dump( in_array(array('','i'), $sub_array) );
                     27: 
                     28: /* checking for objects in in_array() */
                     29: echo "\n*** Testing objects with in_array() ***\n";
                     30: class 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
                     39: var_dump( in_array("array_var", $in_array_obj) ); 
                     40: //error: as wrong datatype for second argument
                     41: var_dump( in_array("foo", $in_array_obj) ); 
                     42: //element found as "one" exists in array $array_var
                     43: var_dump( in_array("one", $in_array_obj->array_var) ); 
                     44: 
                     45: echo "Done\n";
                     46: ?>
                     47: --EXPECTF--
                     48: *** Testing sub-arrays with in_array() ***
                     49: bool(true)
                     50: bool(true)
                     51: bool(true)
                     52: 
                     53: *** Testing objects with in_array() ***
                     54: 
                     55: Warning: in_array() expects parameter 2 to be array, object given in %s on line %d
                     56: NULL
                     57: 
                     58: Warning: in_array() expects parameter 2 to be array, object given in %s on line %d
                     59: NULL
                     60: bool(true)
                     61: Done

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>