array_search

Returns the key of the first array element matching a value, or false if it is not found.

Signature
array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false
  • Added in PHP 4.0.5

Parameters

ParameterTypeDescription
$needlemixedThe value to look for. String comparison is case-sensitive.
$haystackarrayThe array to search in.
$strictoptionalbool= falseWhen true, values are compared with === so type must match as well as value.

Return value

int|string|falseThe key of the first matching element, or false if $needle is not found.

Errors

Throws a TypeError if $haystack is not an array.

Changelog

  • PHP 8.0Non-strict comparison between a string and a number changed: the number is now converted to string when the string is not numeric, so array_search('abc', [0]) no longer returns 0.

array_search() scans an array from the first element to the last and gives you the key of the first value that matches. If nothing matches, it returns false.

When to reach for it

Use it when you need the position, not just a yes/no answer — to remove an item with unset(), to reorder around it, or to find which key in a lookup table holds a value. If you only care whether the value exists, in_array() says so more clearly. If you need every matching key rather than the first, use array_keys($arr, $value).

What trips people up

The return value must be tested with !== false. A hit at position 0 returns integer 0, which is falsy, so if (array_search(...)) silently treats the first element as "not found". This is the single most common bug with this function.

The default comparison is loose. Searching for '1' can match integer 1, and searching for 0 in an array of non-numeric strings behaved surprisingly before PHP 8. Pass true as the third argument whenever types matter — with mixed data it is almost always the right default.

Search is O(n). For repeated lookups, flip the array once with array_flip() and use key access instead.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Find the index of an item in a list so you can unset() or splice it out.
  • Map a database status value back to its label key in a constant lookup array.
  • Locate the current page in an ordered menu array to compute the previous and next links.

Frequently asked questions

What is the difference between array_search and in_array?

array_search returns the key of the first match (or false), while in_array returns only true or false. Use in_array when you just need to know whether a value is present.

Why does array_search return 0 instead of false?

Because 0 is a real key: the value was found at the first position. Test the result with !== false so a legitimate 0 is not mistaken for a failed search.

Does array_search find all matching keys?

No, it returns only the first match in array order. Use array_keys($array, $value) to get every key whose value matches.

More examples

Removing an item by value

unset() leaves a hole: the array still has keys 0, 1, 3. array_values() repacks it into a clean list, which matters if the result is going to json_encode().

Example · php
<?php
$tags = ['php', 'sql', 'html', 'css'];
$key = array_search('html', $tags, true);
if ($key !== false) {
    unset($tags[$key]);
}
print_r($tags);
print_r(array_values($tags));

Strict mode stops type juggling

The loose search treats the string '10' as equal to integer 10 and returns key 0. Strict mode compares type as well and finds nothing. The last call also returns false because none of the values is identically null.

Example · php
<?php
$ids = [10, 20, 30];
var_dump(array_search('10', $ids));        // int(0) - loose match
var_dump(array_search('10', $ids, true));  // bool(false) - string !== int

$flags = [false, 0, ''];
var_dump(array_search(null, $flags, true));

First match only

array_search stops at the first hit and returns 0. array_keys() with a search value returns every matching key (0, 2, 3), which is what you want when duplicates are expected.

Example · php
<?php
$votes = ['yes', 'no', 'yes', 'yes'];
var_dump(array_search('yes', $votes));
print_r(array_keys($votes, 'yes'));
echo count(array_keys($votes, 'yes')), " yes votes\n";

Discussion

  • Be the first to comment on this lesson.