PHP Array Search: A Comprehensive Guide
PHP Array Search is a powerful function that allows you to find the position of a specific value in an array. It is a vital tool for PHP developers and is
array_search() finds the first element in an array equal to a given value and returns that element's key. If nothing matches, it returns false. Use it when you have a value and need to know where it lives in the array — for example, the index you'd pass to unset(), array_splice(), or a follow-up lookup.
It works on both indexed and associative arrays, and it returns the real key — which can be a 0, a non-sequential integer, or a string. This page covers the syntax, loose vs. strict matching, the false-vs-0 gotcha, finding every match (not just the first), and when to reach for in_array() or array_keys() instead.
Syntax
array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false$needle— the value you are searching for.$haystack— the array to search in.$strict— optional. Whenfalse(the default), values are compared loosely (==), so0,"0",0.0, and evenfalsecan match each other. Whentrue, comparison is strict (===), matching only when both type and value are identical. Always passtruewhen the array contains mixed types.
The function returns the key of the first matching element, or false if the value is not present.
Basic usage
The simplest case: search an indexed array and print the key where the value lives.
Here 3 sits at index 2, so the script prints Value was found at key: 2. Note that array_search() returns the actual key, not the position — for a re-indexed or associative array those are not the same thing.
Searching associative arrays
array_search() is most useful on associative arrays, where it gives you back a string key you can use to fetch related data:
<?php
$users = [
"admin" => "Alice",
"editor" => "Bob",
"viewer" => "Cara",
];
$role = array_search("Bob", $users);
echo "Bob's role is: $role"; // editor
?>This prints Bob's role is: editor. If you only need to know whether a value exists — not its key — prefer in_array(), which is clearer and slightly faster.
Strict vs. loose comparison
By default array_search() compares loosely (==), which can produce surprising matches when types differ. Setting the third argument to true forces strict (===) comparison so the type must match too:
In this example, we have created an array called $array that contains five elements, including a string value "3". We then set the value that we want to search for in the array to 3. The array_search() function is then used to search for the value in the array, with the strict parameter set to true. The result is stored in the $result variable. Finally, we use an if statement to check if the value was found in the array or not. If the value was found, the key is outputted. If the value was not found, a message indicating that the value was not found in the array is displayed. Note that array_search() returns false when the value is not found. Because 0 is a valid key, you must use strict comparison (=== false) to correctly distinguish between a not-found result and a key of 0.
The false vs. 0 trap
The single most common bug with array_search() is misreading its return value. Because a found element can legitimately have key 0, and "not found" returns false, a loose check like if (!$result) treats both the same. Always compare with === false:
<?php
$names = ["Alice", "Bob", "Cara"];
// Alice is at key 0 — a real result, but falsy!
$key = array_search("Alice", $names);
if ($key === false) {
echo "Not found";
} else {
echo "Found at key: $key"; // Found at key: 0
}
?>This prints Found at key: 0. A naive if (!$key) would have wrongly reported "Not found".
Finding every matching key
array_search() stops at the first match. To get the keys of all elements equal to a value, use array_keys() with its optional search argument:
<?php
$scores = [10, 20, 30, 20, 50];
echo array_search(20, $scores); // 1 (first match only)
echo "\n";
print_r(array_keys($scores, 20)); // Array ( [0] => 1 [1] => 3 )
?>array_search() returns 1, while array_keys($scores, 20) returns every index holding 20 (1 and 3).
When to use what
| Goal | Best function |
|---|---|
| Get the key of the first matching value | array_search() |
| Just check whether a value exists | in_array() |
| Get all keys for a value | array_keys() |
| Check whether a key exists | array_key_exists() |
Summary
array_search() returns the key of the first element equal to your value, or false if there is none. Remember the essentials: pass strict = true when the array mixes types, always test the result with === false, and reach for in_array() or array_keys() when you need a yes/no answer or every matching key.