How to tell if an element is present in an associative array PHP

As a developer, you’ll frequently be using arrays in your code. Hence, it is always necessary to know the basic array operations. Every programming language provides useful functions to support operations like looping through an array, merging them, or converting them to string.

Searching an array is a fundamental operation, and PHP has several functions for it. Recall that an associative array has key-value pairs. In this article, we will learn how to check if an element is present in an associative array  PHP. We will explore how to search for a specific key or a value. So, let’s begin without any further ado.

Option#1- Check if an element is present in an associative array PHP using foreach loop

That’s the most rudimentary technique to search for an element in an array. We use foreach loop to search for a specific value. Let’s see how.

Example – Searching a specific value

<?php
$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
//Loops through the array.
foreach($employees_array as $key=>$value)
{
    //If Stacie is present. We use a strict type check here.
    if($value === "Stacie")
    {
        echo "Employee Stacie is present in the array";
    }
}
 
//OUTPUT: Employee Stacie is present in the array

?>

The foreach loop checks the array for “Stacie” and finds it in the array. Observe we have used === instead of == in the IF statement. The === compares for identical elements. Identical elements are those with the same value and type. 

This difference between === and == would be obvious when we compare, let’s say integer value 10 and a string value “10”. Comparing the two, == returns true because the value matches while === returns false because the type doesn’t match.

That was a bit off the topic. Returning to the main topic, we can use the foreach loop to check a specific key value in an array.

Example – Searching a specific key

<?php
$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
//Loops through the array.
foreach($employees_array as $key=>$value)
{
    //If Employee#4 is present. We use a strict type check here.
    if($key === "Employee#4")
    {
        echo "Employee#4 is there and goes by the name ".$value;
    }
}
 
//OUTPUT: Employee#4 is there and goes by the name Anna
 
?>

With a bit of variation, we have made this code search a specific key. This way it’s more convenient and straightforward to search if an element is present in an associative array PHP. However, it is not recommended to reinvent the wheel. That’s why we will see a built-in function for this array operation.

Option#2- Check if an element is present in an associative array PHP using in_array function

PHP in_array function searches an array for a specific value. It is a one-liner and significantly reduces the code we’d in the previous section. For a good headstart, check out the PHP documentation for this function.

Example – Searching a specific value

<?php
$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
    //Checks if Matthew is present in the array.
    if(in_array("Matthew",$employees_array))
    {
        echo "Employee Matthew is present in the array";
    }
    
    else
    {
        echo "Employee Matthew is not present in the array";
    }

//OUTPUT: Employee Matthew is present in the array
?>

Comparing this code to the previous section, it is shorter and more compact. We can also use in_array function to look for a specific key value. The only addition would be retrieving the keys array through the array_keys function and then searching through this array.

Example – Searching a specific key

<?php
$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
$employees_keys = array_keys($employees_array);
    //Checks if Employee#5 is present in the array.
    if(in_array("Employee#5",$employees_keys))
    {
        echo "Employee#5 exists";
    }
    
    else
    {
        echo "Employee#5 doesn't exist";
    }
      
    

//OUTPUT: Employee#5 exists

?>

Perfect! Let’s see another function for checking an element in an array.

Option#3- Check if an element is present in an associative array PHP using array_search function

PHP array_search function searches an array for a value and returns the first corresponding key if the search is successful. We can use it for the purpose of checking a specific value. The example shows it.

Example – Searching a specific value

<?php
$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
 
    //Checks if Robert is present in the array.
    if(array_search("Robert",$employees_array) !== false)
    {
        echo "Employee Robert is present in the array";
    }
    
    else
    {
        echo "Employee Robert is not present in the array";
    }
     
//OUTPUT: Employee Robert is present in the array
 
?>

Here’s how to use it to check for specific keys.

Example – Searching a specific value

<?php
$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
$employees_keys = array_keys($employees_array);
    //Checks if Employee$6 is present in the array.
    if(array_search("Employee#6",$employees_keys))
    {
        echo "Employee#6 exists";
    }
    
    else
    {
        echo "Employee#6 doesn't exist";
    }
   
//OUTPUT: Employee#6 exists
 
?>

Uses a similar method of searching through a keys array. Great! Let’s see one last function before ending this article.

Option#4- Check if an element is present in an associative array PHP using array_key_exists function

PHP array_key_exists function is specialized for searching specific key values in an array. Let’s have a quick look at it through an example.

<?php
$employees_array = array(
    "Employee#1" => "Bob",
    "Employee#2" => "Stacie",
    "Employee#3" => "Robert",
    "Employee#4" => "Anna",
    "Employee#5" => "Matthew",
    "Employee#6" => "John"
);
 
 
    //Checks if Employee$6 is present in the array.
    if(array_key_exists("Employee#6",$employees_array))
    {
        echo "Employee#6 exists";
    }
    
    else
    {
        echo "Employee#6 doesn't exist";
    }
      
 
//OUTPUT: Employee#6 exists
?>

With this function, we don’t need to call the array_keys function. The array_key_exists searches the array for the key. 

That’s pretty much it. Now, there are other ways to check if an element is present in an array but having these under your belt, you are good to go. That’s the end of this article and we hope you’ve enjoyed it. Check out our website for more informative content related to PHP.

Want to explore further about PHP arrays?

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

Remove element from an associative array in PHP

How to split associative arrays in PHP

How to convert associative array to csv in PHP


Article Categories

Article Tags

© 2023 Confident.Systems