PHP array_search() function


The array_search() function in PHP is used to search for a specified value in an array and return the corresponding key if the value is found. If the value is not found, the function returns false. This function is particularly useful for checking the presence of a value in an array and retrieving its key.

Syntax:

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

Parameters:

  • $needle: The value to search for in the array.
  • $haystack: The array in which to search for the specified value.
  • $strict: (Optional) A boolean parameter that determines whether to perform a strict type comparison. If set to true, array_search() will check both the value and the type. If set to false (the default), it will only check the value.

Return Value:

  • Returns the key of the first matching value if found. If the value is not found, it returns false. If the key of the value is 0, it will return 0, which is considered falsy in PHP.

Example 1: Basic Search

In this example, we demonstrate how to use array_search() to find a value in an array.

<?php $array = ["apple", "banana", "cherry"]; $key = array_search("banana", $array); echo "Key: " . $key . "\n"; // Output: Key: 1 ?>

Output:

Key: 1

In this case, the value "banana" is found at key 1.

Example 2: Value Not Found

If the value you are searching for is not in the array, array_search() will return false.

<?php $array = ["apple", "banana", "cherry"]; $key = array_search("date", $array); echo "Key: " . ($key === false ? 'Not found' : $key) . "\n"; // Output: Key: Not found ?>

Output:

Key: Not found

Here, the value "date" is not found, so the output indicates that the key was not found.

Example 3: Strict Search

Using the strict parameter can affect the search results, especially when dealing with types.

<?php $array = [0, "1", 2]; $key = array_search(1, $array, true); // Strict comparison echo "Key: " . ($key === false ? 'Not found' : $key) . "\n"; // Output: Key: Not found ?>

Output:

Key: Not found

In this example, 1 is not found in the array because of strict type checking. The value 0 (integer) is not the same as "1" (string), so the function does not find a match.

Example 4: Key of Zero

If the value is found at key 0, it will return 0, and you need to check against false explicitly.

<?php $array = [0 => "zero", 1 => "one", 2 => "two"]; $key = array_search("zero", $array); echo "Key: " . ($key === false ? 'Not found' : $key) . "\n"; // Output: Key: 0 ?>

Output:

Key: 0

In this case, the function successfully finds the value "zero" at key 0.

Practical Usage:

  • array_search() is useful for:
    • Determining if a specific value exists in an array and retrieving its key.
    • Searching through associative arrays where values may not be unique.
    • Implementing features such as finding the index of an item in a list or checking membership.

Summary:

  • array_search($needle, $haystack, $strict) searches for a value in an array and returns the corresponding key if found.
  • The function can perform strict type comparisons when needed, which affects the search results.
  • It is a valuable function for managing and querying arrays in PHP, especially when working with associative arrays.