Find Array Keys with array_key_exists: 3 PHP Code Examples

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Find a key in an array PHP

An array in PHP keeps “KEY/VALUE” pairs, realizes the generic hash map. PHP features a variety of functions to work with the keys and values in a PHP array. There are functions for sorting, splitting, adding, removing values, and many other array functions. Similarly, there are functions for searching keys or values in an array.

array PHP

This article explores several options for finding a key in an array in PHP. Stick to the article till the end to learn these options and pick the one that fits well in your code.

Let’s dive straight into the options for finding a key in a PHP array.

#1 – Find a key in an array PHP using the foreach loop

A foreach loop iterates over the array and references the key and the value. That’s why we can leverage it to find a key in an array. Here is how it is done.

<?php
 
$name_age = [
    "David" => 30,
    "Martha" => 43,
    "Tyson" => 18,
    "Sarah" => 22,
    "Ralph" => 19
];
 
function findKey($arr, $key) {
 
    foreach($arr as $k => $v) {
        if($k === $key) {
            return true;
        }
    }
 
    return false;
}
 
print_r(findKey($name_age, "David")); //true
print_r(findKey($name_age, "Brian")); //false
print_r(findKey($name_age, "Ralph")); //true
 
?>

So, the function has a foreach loop that iterates over the array and matches the key with the search value. When it finds a match, it returns true. Otherwise, it returns false. There’s a function in PHP that does the same thing. This function is array_key_exists.

#2 – Find a key in an array PHP using the array_key_exists

The array_key_exists function takes two arguments, a key value to search for and the array to search into. The functionality is just the same as the previous example.

<?php
 
$name_age = [
    "David" => 30,
    "Martha" => 43,
    "Tyson" => 18,
    "Sarah" => 22,
    "Ralph" => 19
];
 
print_r(array_key_exists("David", $name_age)); //true
print_r(array_key_exists("Brian", $name_age)); //false
print_r(array_key_exists("Ralph", $name_age)); //true
 
?>

Voila! The output remains the same as in the last example. There is yet another option to find a key in an array PHP.

#3 – Find a key in an array using the array_search

The array_search function searches a value in the array and returns its corresponding key. There’s a way to use this function to look for a key in an array. The array_search function can be coupled with the array_keys function. The array_keys function returns an array of the keys of the input array. 

If we input the returned array from the array_keys to array_search, it will return the numeric index. The numeric index coerces to a boolean value, implying that the function exists. Here’s how it works.

<?php
 
$name_age = [
    "David" => 30,
    "Martha" => 43,
    "Tyson" => 18,
    "Sarah" => 22,
    "Ralph" => 19
];
 
print_r(array_search("David", array_keys($name_age))); //0
print_r(array_search("Brian", array_keys($name_age))); //false
print_r(array_search("Ralph", array_keys($name_age))); //4
 
 
?>

See! It returns the numeric index for the keys it finds and returns false for those it doesn’t find. The index numbers can be a source of confusion, but the array_keys function returns an array with numeric indexes that look like this.

Array
(
    [0] => David
    [1] => Martha
    [2] => Tyson
    [3] => Sarah
    [4] => Ralph
)

Conclusion

This article explores different options for finding a key in an array PHP. The first option uses a foreach loop to iterate over an array and find a key. The second approach uses the array_key_exists function, and lastly, the array_search function couples with the array_keys to find a key in an array in PHP.

We hope that you’ve learned something new today. Stay tuned for more exciting articles and tutorials related to PHP.

Want to explore more useful PHP tutorials?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

How to merge 2 arrays with the same keys in PHP

Difference between array_combine and array_merge in PHP

PHP array_search: How to use in PHP with examples

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.