Search the array for the key "red" and return its key name:
<?php$a=array("a"=>"red","b"=>"green","c"=>"blue");echo array_search("red",$a);?>The array_search() function searches for a key value in the array and returns the corresponding key name.
array_search( value,array,strict )
parameter | describe |
---|---|
value | Required. Specifies the key value to search for in the array. |
array | Required. Specifies the array to be searched. |
strict | Optional. If this parameter is set to TRUE, the function searches the array for elements with the same data type and value. Possible values: true false - default If set to true, the type of the given value is checked in the array and the number 5 and the string 5 are different (see Example 2). |
Return value: | If the specified key value is found in the array, the corresponding key name is returned, otherwise FALSE is returned. If a key value is found more than once in the array, the key name matching the first found key value is returned. |
---|---|
PHP version: | 4.0.5+ |
Update log: | If invalid parameters are passed to the function, the function returns NULL (this applies to all PHP functions since PHP 5.3.0). As of PHP 4.2.0, if the search fails, this function returns FALSE instead of NULL. |
Search for the key value 5 in the array and return its key name (note the ""):
<?php$a=array("a"=>"5","b"=>5,"c"=>"5");echo array_search(5,$a,true);?>