PHP array_search() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_search() looks for a value in an array and returns the key of the first match.

If the value is found you get its key (a number for lists, or a string for associative arrays). If it is not found you get false, so always compare the result with === false.

Syntax

Call array_search() with the values you need. Start with this line.

PHP
$key = array_search("banana", $fruits);
CallWhat it does
array_search("banana", ["apple", "banana"])Returns 1, the index of "banana".
array_search("red", ["a" => "red", "b" => "blue"])Returns "a", the key that holds "red".
array_search("grape", ["apple", "banana"])Returns false because "grape" is not in the array.

array_search() is like in_array(), but it tells you where the value is instead of only saying yes or no. Use the third argument set to true when you need a strict type match.

Library declarations
PHP
array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false

Parameters

ParameterDescription
needleRequired. The value to look for.
haystackRequired. The array to search in.
strictOptional. If true, PHP also checks the type (so 1 and "1" are different). The default is false.

Return Value

Return ValueDescription
int|stringThe key of the first matching value.
falseReturned when the value is not found.

PHP Examples

Example 1 — Find the Key of a Value

Search a list of fruits and print the index of the match.

PHP
<?php
$fruits = ["apple", "banana", "cherry"];
$pos = array_search("banana", $fruits);

echo "Index: ", $pos, PHP_EOL;

How It Works

  1. Create a list of three fruits. Their keys are 0, 1 and 2.
  2. array_search() scans the list from the start for "banana".
  3. It finds the match at key 1 and returns that key.
  4. echo prints the key.

Example 2 — Key 0 and Missing Values

The first item has key 0, which can be confused with false. Compare with === to be safe.

PHP
<?php
$letters = ["a", "b", "c"];

$pos = array_search("a", $letters);
if ($pos === false) {
    echo "Not found", PHP_EOL;
} else {
    echo "Found at index ", $pos, PHP_EOL;
}

$missing = array_search("z", $letters);
var_dump($missing);

How It Works

  1. array_search("a", . ..) returns 0 because "a" is the first item.
  2. The strict check $pos === false is false, so the else branch runs.
  3. Searching for "z" finds nothing, so the function returns false.
  4. var_dump() shows the exact value: bool(false).

Example 3 — Loose vs Strict Search

The third argument decides whether types must match.

PHP
<?php
$numbers = [10, "20", 30];

echo array_search(20, $numbers), PHP_EOL;
var_dump(array_search(20, $numbers, true));

How It Works

  1. The array holds the number 10, the string "20" and the number 30.
  2. A loose search treats the number 20 and the string "20" as equal, so it returns key 1.
  3. With strict set to true, the integer 20 does not match the string "20".
  4. No strict match exists, so PHP returns false.

Frequently Asked Questions

It returns the key of the first matching value. If nothing matches, it returns false.
in_array() only tells you true or false. array_search() tells you where the value is by returning its key.
When the match is the first item, the key is 0, and 0 counts as false in an if. Use !== false to check for a real match.
No. It stops at the first match. To get every matching key, use array_keys() with a search value.
array_search() is case-sensitive. A common trick is to lowercase both sides, for example array_search(strtolower($word), array_map('strtolower', $list)).

Next: array_shift()

array_shift() removes the first item from an array and returns it.

array_shift() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful