PHP » Arrays » array_search()

Syntax:
mixed array_search(mixed search, array array [, bool strict])
search
Element value to search for.
array
The array to be searched.
strict
Whether to return keys only for elements of the same type as the search term.

Searches an array for an element.

The function array_search() searches an array for an element. If it is found, the index or key is returned. Otherwise, it returns FALSE. If more than one element matches, the first is returned.

Examples

Code:
<?php

$array1 = array("one", "two", "three");

$array2 = array(
   "un" => "one",
   "deux" => "two",
   "trois" => "three");


print "Print index of \"two\": " . array_search("two", $array1, TRUE) . "<br>";
print "Two in French is: " . array_search("two", $array2, TRUE);

?>
Output:
Print index of "two": 1
Two in French is: deux
Explanation:

The code shows array_search() in use on an index and an associative array.

See Also: