Searching PHP Arrays

PHP provides several functions that can be used to search arrays, including array_search, array_keys (when passed a search value), in_array, and array_key_exists. We describe and demonstrate each of these functions on this page.

array_search

Pass a value and an array to the array_search function and it will return the first key with a matching value. Or, if no match is found, it will return false. We demonstrate here with a numerically indexed array:

$ar = ['apple', 'orange', 'pear', 'grape'];
// search for 'apple' in $ar
var_dump( array_search('apple', $ar) ); // int(0) 

The array_search function returns 0 in the example above since a match is found at the beginning of the array. A strict check[1] of the return value is needed due to this possibility:

// strict check for false using !== or ===
if ( array_search('apple', $ar) !== false ) {
    echo 'value found';
}

When array_search finds a matching value in an associative array, its string key is returned:

$food_colors = [
    'apple' => 'red',
    'grape' => 'purple',
    'tomato' => 'red',
    'kale' => 'green'
];

// search for value 'red' in $food_colors
var_dump( array_search('red', $food_colors) ); // string(5) "apple"

The array_search function only returns the key for the first match. The array_keys function, described below, can be used to find all the keys with matching values.

Strict Comparison Argument

The array_search function provides an optional third argument to specify whether a strict comparison of values should be performed. The following demonstrates what can happen if it is not included:

$ar = [true, false, 0, 1, ''];

// search for 'apple' in $ar
var_dump( array_search('apple', $ar) ); // int(0)

// search for 'apple' with strict comparison
var_dump( array_search('apple', $ar, true) ); // bool(false)

When no third argument is passed, the default is false and type conversion will take place. This can result in false positives. Pass true as the third argument so no type conversion will take place when the values are compared.

The array_keys and in_array functions also provide arguments for strict comparison. It is a good idea to use them unless you are sure of your data types, or type conversion is needed for your search.

Using the array_keys Function for Search

The array_keys function typically returns all the keys in an array. But if you pass a search value as the second argument, it will return the keys that match that value:

$food_colors = [
    'apple' => 'red',
    'grape' => 'purple',
    'tomato' => 'red',
    'kale' => 'green'
];

// arguments: array, search value (optional)
print_r( array_keys($food_colors, 'red') );
/* print_r output (as seen in page source view):
Array
(
    [0] => apple
    [1] => tomato
) */

When a search value is passed to the array_keys function, it returns an array containing all the keys with matching values. Pass true as the third argument for strict comparison of the search value.

in_array

Pass a value and an array to the in_array function and it will return true or false to indicate whether the value exists in the array. We demonstrate here:

$ar = ['Jon', 'Mark', 'Jess'];
// search for 'Jess' in $ar above
var_dump( in_array('Jess', $ar) ); // bool(true)
// search for 'jess' returns false
var_dump( in_array('jess', $ar) ); // bool(false)

As you can see from the results above, the search is case-sensitive.

The following demonstrates a typical way to inspect the return value of the in_array function:

if ( in_array('Jess', $ar) ) { 
    // code ...
}

An optional third argument can be used to specify a strict comparison of values. The default is false, but you can pass true to avoid type conversion when values are compared.

array_key_exists

Pass a key and an array to the array_key_exists function and it will return true or false to indicate whether that key is present in the array. We demonstrate here:

$person = array(
    'name' => 'Jon',
    'age' => 26,
    'marital status' => null,
    'friends' => array('Matt', 'Kaci', 'Jess')
);

var_dump( array_key_exists('marital status', $person) ); // bool(true)

Even if the value is null, the array_key_exists function will return true as long as the key is present in the array. You can use the isset language construct to determine if a value is null. If so, it will return false:

var_dump( isset($person['marital status']) ); // bool(false)

Back to top


  1. Strict comparisons using === and !== check both value and type, while comparisons using == and != will perform type conversions when comparing 0 with false, for example. ^