php array search array_search function

PHP Array Search Function (5 Methods)

On: Sep 19, 2022 / By: Honar Systems / Categories: Used before category names. PHP

In PHP, we have three commonly used array types: one is indexed, the other is associative, and the other is the multidimensional array. Various functions can be used to search an array in PHP; one of the most important and famous search functions is the array_search() function.

If you are not familiar with arrays in PHP, we have provided an article for you. Or you can view arrays from the official PHP website.

Searching in arrays can be done in several ways, such as searching by value, searching by key, searching in associative arrays, etc.

There is also a type of array searching that uses iteration loops in PHP. However, it is less efficient, but we will cover this method in this article.

In this tutorial, we will learn how to search for a value in an array in PHP, and we will explore various methods and techniques to search.

PHP array search content table

This function has existed since PHP version 4, and using this function, we can easily search within an array.

The PHP array_search() function searches the array for a given value. It returns the first corresponding key if the search was successful.

It performs a loose or strict search based on parameters and returns false if the value does not exist in the array.

This function receives 3 variables as input parameters and searches within the array given as parameters. If the value is found in the array, it returns its key; if the given value does not exist, it returns false.

If the array is indexed, it returns its index number, and if it is associative, it returns the key of the found item.

array_search(search_term, array, strict);
search_termRequiredThe searched value
arrayRequiredThe array
strictOptionalIf this parameter is set to “true”, then the array_search() function will search for identical elements in the array. This means it will also perform a strict type comparison of the search_term in the array, and objects must be the same instance.
Returned ValueReturns the key for search_term if it is found in the array; if the search action isn’t successful, then it returns “false”.

This function is case sensitive, so the value in the array must match the value you enter.

If there is more than one element that matches the search term, the first matching key is returned.

Note: This function may return the boolean value false, but it can also return a non-boolean value that evaluates to false, such as 0. Use the “===” operator to check the return value of this function in conditions.

$cars = array(
    'fer' => 'Ferrari', 
    'ben' => 'Benz', 
    'bmw' => 'BMW', 
    'vol' => 'Volvo'
);

var_dump(array_search('Benz', $cars));

Output

ben

If there are 2 values ​​that have the value Benz, the function returns the first key value found.

Case-insensitive search

As mentioned, this function is case sensitive and returns false if the entered value is not exactly the same as the value in the array.

To make it non-case-sensitive, we use a combination of the array_map function and the strtolower function.

$cars = array(
    'fer' => 'Ferrari', 
    'ben' => 'Benz', 
    'bmw' => 'BMW', 
    'vol' => 'Volvo'
);

var_dump(array_search(strtolower('benz'), array_map('strtolower', $cars)));

Output

ben

The array_keys() function to search an array by key in PHP

In PHP, the array_search() function returns the first key of the array used search_term if it is found. To return the keys for all matching values, use the array_keys() function with the optional search_term parameter instead.

array_keys(array, search_term);

The search_term parameter is optional. If you do not set the search_term parameter, the function returns all the keys in the array.

However, if you set the parameter, the function returns the key value of the search_term parameter.

If the searched value is not present in the array, the function returns an empty value.

$cars = array(
    'fer' => 'Ferrari', 
    'ben' => 'Benz', 
    'bmw' => 'BMW', 
    'ben2' => 'Benz'
);

var_dump(array_keys($cars, 'Benz'));

Output

array (size=2)
  0 => string 'ben' (length=3)
  1 => string 'ben2' (length=4)

As you can see in the example, if the searched value is present in two items, unlike the array_search() function, it returns the key of both items.

array_key_exists() function

Up until this point, we’ve discussed how to search array values ​​in PHP, but if you want to search within the array keys, you can use this function.

array_key_exists() is a built-in PHP function to check if the given key exists in the array or not. This function returns “true” if the given key value is set in the array.

$cars = array(
    'fer' => 'Ferrari', 
    'ben' => 'Benz', 
    'bmw' => 'BMW', 
    'vol' => 'Volvo'
);

var_dump(array_key_exists('ben', $cars));

Output

true

Note: This function only applies to associative arrays.

You can also search within arrays using loops in PHP. This is not the most efficient way, but it can be used in special cases.

$cars = array(
    'fer' => 'Ferrari', 
    'ben' => 'Benz', 
    'bmw' => 'BMW', 
    'vol' => 'Volvo'
);

$found_key = false;
foreach($cars as $key => $car){
    if($car === 'Benz'){
        $found_key = $key;
        break;
    }
}

var_dump($found_key);

Output

ben

If the array is indexed instead of associative, it returns the index value of the found item.

How to search in multidimensional array in PHP?

Using the array_search() function, you can only search in one-dimensional arrays. To search in multi-dimensional arrays, you must use the array_column() function together with the array_search() function.

$cars = array(
    'fer' => ['name' => 'Ferrari', 'country' => 'Italy'],
    'ben' => ['name' => 'Benz', 'country' => 'Germany'],
    'bmw' => ['name' => 'BMW', 'country' => 'Germany'],
    'vol' => ['name' => 'Volvo', 'country' => 'Sweden']
);

var_dump(array_search('Benz', array_column($cars, 'name')));

Output

1

In this method, to return the key of an array item, it returns its index, which in this example is 1.