How to Check if a Key Exists in an Associative Array in PHP?
Last Updated : 16 Jul, 2024
Given an Associative array, the task is to check whether a key exists in the associative array or not. There are two methods to check if a key exists in an associative array, these are - using array_key_exists(), and isset() functions. Let's explore each method with detailed explanations and code examples.
Approach 1: Using array_key_exists() Function
The array_key_exists() function checks if a specified key exists in an array. It returns true if the key exists and false otherwise.
PHP <?php $student_marks = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); // Check if the key exists in the array if (array_key_exists('Physics', $student_marks)) { echo "Key exists in the array"; } else { echo "Key does not exist in the array"; } ?>
OutputKey exists in the array
Approach 2: Using isset() Function
The isset() function checks if a variable is set and is not null. When used with an array, it checks if a specified key exists in the array and is not null.
PHP <?php $student_marks = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); // Check if the key exists in the array if (isset($student_marks['Physics'])) { echo "Key exists in the array"; } else { echo "Key does not exist in the array"; } ?>
OutputKey exists in the array
Approach 3: Using array_key_first() Function
The array_key_first() function returns the first key of the given array. We can use this function to determine if a key exists by iterating over the keys of the array.
PHP <?php $student_marks = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); // Check if the key exists in the array function keyExists($key, $array) { foreach (array_keys($array) as $k) { if ($k === $key) { return true; } } return false; } if (keyExists('Physics', $student_marks)) { echo "Key exists in the array"; } else { echo "Key does not exist in the array"; } ?>
OutputKey exists in the array
Approach 4 : Looping through Keys
To check if a key exists in an associative array in PHP, iterate through array keys using a foreach loop. Compare each key against the target key. Set a flag upon finding a match to confirm existence or absence of the key.
Example:
PHP <?php // Associative array $assocArray = [ "key1" => "value1", "key2" => "value2", "key3" => "value3", ]; // Key to search for $findKey = "key2"; // Flag to indicate key existence $keyExists = false; // Loop through keys foreach ($assocArray as $key => $value) { if ($key === $findKey) { $keyExists = true; break; } } // Output results if ($keyExists) { echo "Key '$findKey' exists in the associative array."; } else { echo "Key '$findKey' does not exist in the associative array."; } ?>
OutputKey 'key2' exists in the associative array.
Approach 5: Using the in_array() Function with array_keys()
The in_array() function checks if a value exists in an array. By combining it with the array_keys() function, which returns all the keys of an array, you can check if a specific key exists.
Example
PHP <?php $array = array( "first" => 1, "second" => 2, "third" => 3 ); $keyToCheck = "second"; if (in_array($keyToCheck, array_keys($array))) { echo "Key '$keyToCheck' exists in the array."; } else { echo "Key '$keyToCheck' does not exist in the array."; } ?>
OutputKey 'second' exists in the array.
Similar Reads
How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
2 min read
Sort an Associative Array by Key in PHP Given an Associative Array, the task is to sort the associative array by its keys in PHP. There are different methods to sort Associative Array by keys, these are described below: Table of ContentUsing ksort() FunctionUsing uksort() FunctionConverting to a Regular Array for SortingUsing array_multis
3 min read
How to check if an Array contains a value or not? There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-b
3 min read
PHP array_key_exists() Function The array_key_exists() function in PHP is a built-in function that checks whether a specified key exists in an array. This function is commonly used when working with associative arrays, where keys are explicitly defined, and we need to confirm the presence of a particular key to prevent errors or u
2 min read
How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar
6 min read
How to create an array with key value pairs in PHP? In PHP, an array with key-value pairs is called an associative array. It maps specific keys to values, allowing you to access elements using custom keys rather than numerical indices. Keys are strings or integers, while values can be of any data type.Here we have some common approaches to create an
2 min read