PHP array_search function
Last Updated: February 15, 2022
array_search — Search the array for a given keyword and return the key
Syntax
array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false
Supports (PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)
Parameters
Parameter | Description |
needle | Search value |
haystack | array to be searched |
strict | If the third parameter strict is set to true then the array_search() function will search for identical elements in the haystack. This means it will also perform a strict type comparison of the needle in the haystack, and objects must be the same instance. |
Return Value
returns the key if the searched value is found otherwise it returns false
Example
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;