PHP array_search() Function

Usage — The PHP array_search() function is used to search the array for a given value and returns the first corresponding key if the search was successful.

It has the following syntax:

PHP

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

Here is an example of using array_search():

PHP

$pairs = array("Amanda"=>"John", "Bran"=>"Harry", "Mary"=>"Manisha");

// Output — string(4) "Mary"
var_dump(array_search("Manisha", $pairs));

// Output — bool(false)
var_dump(array_search("Mary", $pairs));

Return Value — This function returns the key for needle if it is found in the array, FALSE otherwise.

Additional Information — If the needle is found in haystack more than once, the first matching key is returned. You can also return the keys for all matching values, using array_keys() with the optional search_value parameter instead.

Please take a note that this function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Therefore, you should use the === operator for testing the return value of this function.

PHP Version and Changelog — The array_search() function is available in PHP 4 >= 4.0.5, PHP 5, PHP 7. Starting from PHP version 4.2.0, this function started returning FALSE on failure instead of NULL. Just like all other internal functions of PHP, as of 5.3.0, array_search() also returns NULL if invalid parameters are passed to it.

Relevant Functions — Other related PHP functions that you should know about are: array_keys() which returns all the keys or a subset of the keys of an array, array_values() which returns all the values of an array, array_key_exists() which checks if the given key or index exists in the array and in_array() which checks if a value exists in an array.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

needle

The needle parameter is used to specify the searched value. This is a required parameter. Please note that if the needle is a string, the comparison is done in a case-sensitive manner.

haystack

The haystack parameter is used to specify the array. This is a required parameter.

strict

The strict parameter is a Boolean that is used to specify if the array_search() function will search for identical elements in the haystack. This means it will also perform a strict type comparison of the needle in the haystack, and objects must be the same instance. This is an optional parameter. Its default value is FALSE. When set to true, the number 5 is not the same as the string 5.

Working Examples

Here are some examples of using the array_search() function:

PHP

$collection = array(0 => 'Blue', 1 => 'blUe', 2 => '100', 3 => 100);

// Output — int(1)
var_dump(array_search('blUe', $collection));

// Output — int(0)
var_dump(array_search('Blue', $collection));

// Output — bool(false)
var_dump(array_search('BLUE', $collection));

// Output — int(2)
var_dump(array_search(100, $collection));

// Output — int(3)
var_dump(array_search(100, $collection, true));

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. In (PHP 5 >= 5.5.0) you don’t have to write your own function to search through a multi dimensional array. Here is an example that illustrates how to use array_search() and array_column() to achieve the same result.

    PHP

    $userdb=Array (
    (0) => Array
        (
            (uid) => '100',
            (name) => 'Sandra Shush',
            (url) => 'urlof100'
        ),
        (1) => Array
        (
            (uid) => '5465',
            (name) => 'Stefanie Mcmohn',
            (pic_square) => 'urlof100'
        ),
        (2) => Array
        (
            (uid) => '40489',
            (name) => 'Michael',
            (pic_square) => 'urlof40489'
        )
    );
    
    // Output — 2
    echo array_search(40489, array_column($userdb, 'uid'));
    

    Suggested by - xfoxawy at gmail dot com

  2. If you are using the result of array_search in a condition statement, make sure you use the === operator instead of == to test whether or not it found a match. Otherwise, searching through an array with numeric indices will result in index 0 always getting evaluated as false/null. This nuance cost me a lot of time and sanity, so I hope this helps someone. In case you don’t know what I’m talking about, here’s an example:

    Suggested by - cue at openxbox dot com

  3. You can use the following code for searching in a case insensitive manner.

    PHP

    array_search(strtolower($element),array_map('strtolower',$array));
    

    Suggested by - stefano@takys dot it

Further Reading

  1. You can read more about the PHP array_search() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top