array_search($value, $array) returns the key of the first element equal to $value, or false if no element matches. Because a found key can be 0, test the result with !== false, as in if (($key = array_search('pear', $fruits)) !== false).
If you only need a yes or no, in_array() says so directly. array_search() is for when you need to know where the value sits, for example to change or remove it.
The 0 vs false pitfall
The most common array_search() bug: the value is the first element, its key is 0, and if ($key) treats 0 as false. The code says "not found" for a value that is right there. Compare with !== false, which only matches a real false.
!= false has the same bug as if ($key), because 0 == false is true. Only the strict operator tells them apart.
Strict search
Like in_array(), array_search() compares with == unless you pass true as the third argument. Loose mode lets a string find a number and null find 0, which returns a key you did not expect.
Find all keys with a value
array_search() stops at the first match. array_keys($array, $value) returns every matching key as an array; add true for a strict comparison. An empty result means no match, so there is no 0 vs false confusion.
Search a multidimensional array with array_column
array_search() does not look inside nested arrays. For a list of rows, extract the field you are searching with array_column(); the positions in that list match the row numbers, so the result is the index of the row.
array_column() renumbers its output from 0, so this only lines up when the rows are numbered 0, 1, 2 without gaps. After unset() or array_filter() the numbering has gaps; run array_values() on the rows first or use the keyed lookup in the last two lines. array_column() has more on the index-key form.
Case-insensitive search
array_search() compares strings exactly. Lowercase both sides to ignore case. array_map() keeps the keys, so the key you get back is the key in the original array.
Search and replace or remove
The usual reason to search is to act on the element you found. With the key in hand, assign to change it or unset() to remove it, and always guard with !== false first: unset($arr[false]) silently removes the element with key 0.
Frequently Asked Questions
What does array_search return if the value is not found?
false. When the value is found it returns its key, which may be 0 or an empty string, so check the result with !== false, never with if ($key) or != false.
Why does my array_search check fail for the first element?
The first element of an indexed array has key 0, and 0 is falsy. if (array_search('a', ['a', 'b'])) therefore takes the not-found branch. Write if (array_search('a', $arr) !== false).
How do I find all keys for a value in PHP?
array_search() stops at the first match. array_keys($array, $value, true) returns every key whose value matches, as an array (empty when there is none).
How do I search a multidimensional array in PHP?
Pull the field into a flat list and search that: $i = array_search('ada@example.com', array_column($users, 'email'));, then read the row as $users[$i]. This works when the rows are numbered 0, 1, 2; otherwise loop with foreach.