PHP array_search() Function

The array_search() function in PHP searches an array for a given value and return the associated key of the element if it is located. If the value is not found, the function returns false.

This function is especially useful when you need to locate a specific element within an array, but you only know its value, not its key.

Syntax of array_search() Function in PHP


The basic syntax of array_search() function in PHP is as follows:

array_search(mixed $needle, array $haystack, bool $strict = false): mixed

This function searches the array haystack for the value needle, returning its key if found, and false otherwise. In the above syntax, there are three parameters:

  • $needle (required): This parameter represents the value to search for in the array. It can be of any type, such as string, int, float, array, object, etc.
  • $haystack (required): This parameter is the array in which the function will search for the specified value. It works with both indexed and associative arrays.
  • $strict (optional): If this parameter is set to true, the function will also check the data types of the values, performing a strict comparison (===). In other words, the function will also check the types of the $needle in the haystack.

The array_search() function returns the key for the $needle if located in the array. If not found, it returns false value.

Basic Examples of array_search() Function


Let’s take some basic examples based on array_search() function in PHP.

Example 1:

<?php
$colors = array("red", "blue", "green", "yellow", "orange");
$key = array_search("yellow", $colors);
echo $key;
?>
Output:
      3

In the example, the array_search() function will search the value “yellow” in the $colors array and return the corresponding key 2.


Example 2: Associative Array

<?php
$person = array("name" => "Saanvi", "age" => 25, "city" => "Dhanbad");
$key = array_search(25, $person);
echo $key;
?>
Output:
      age

In this example, the array_search() function searches for the value 30 and returns its corresponding key, which is age.

Example 3: Strict Comparison

<?php
$values = array("1", 2, "3");
$key = array_search(1, $values, true);
var_dump($key);
?>
Output:
      bool(false)

In this example, we have enabled the strict comparison. Therefore, PHP checks both the value and the data type. Since the string “1” does not match the integer 1, hence it returns false.

Case-insensitive Search using array_search() Function


The array_search() built-in function provided by PHP is case-sensitive by default. This means that searching for “red” in an array containing “Red” will not return a match. To perform a case-insensitive search, you need to normalize the case of both the array values and the search term using functions like strtolower() or strtoupper(). Look at the following example code.


Example 4:

<?php
$colors = array("Red", "Green", "Blue", "yellow", "black");
$search = "yellow";

// Convert both the search term and array values to lowercase.
$lowerColors = array_map('strtolower', $colors);
$lowerSearch = strtolower($search);

$key = array_search($lowerSearch, $lowerColors);
echo $key;
?>
Output:
      3

In this example, the original array named $colors contains capitalized color names. The array_map(‘strtolower’, $colors) function creates a new array with all values in lowercase like array(“red”, “green”, “blue”, “yellow”, “black”).

Then, we have used strtolower() function to convert the search term “yellow” into lowercase. After that, the array_search() function finds the index of the lowercase search term in the lowercase version of the array.


The array_search() function provided by PHP is used to find the key of a specific value within an array. I hope that you will have understood the basic use of array_search() function and practiced all examples based on it.