The Wayback Machine - https://web.archive.org/web/20240712113551/https://www.geeksforgeeks.org/how-to-check-a-key-exists-in-an-array-in-php/
Open In App

How to check a key exists in an array in PHP ?

Last Updated : 10 Jul, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We have given an array arr and a Key key, the task is to check if a key exists in an array or not in PHP.

Examples:

Input : arr = ["Geek1", "Geek2", "1", "2","3"] 
key = "2"
Output : Found the Key

Input : arr = ["Geek1", "Geek2", "1", "2","3"]
key = 9
Output : Key not Found

The problem can be solved using PHP inbuilt function for checking key exists in a given array. The in-built function used for the given problem are:

Method 1: Using array_key_exists() Method:

The array_key_exists() function checks whether a specific key or index is present inside an array or not.

Syntax:

boolean array_key_exists( $index, $array )

Example:

PHP
<?php
// PHP program to check if a key 
// exists in an array or not

$array = array(
      'names' => array("Geek1", "Geek2", "Geek3"), 
    'rank' => array('1', '2', '3')
); 

// Use of array_key_exists() function
if(array_key_exists("rank", $array)) {
    echo "Found the Key"; 
} 
else{ 
    echo "Key not Found"; 
} 

?>

Output
Found the Key

Method 2: Using isset() Method:

The isset() function checks whether a specific key or index is present inside an array or not.

Syntax:

bool isset( mixed $var, mixed $... )
PHP
<?php
// PHP program to check if a key 
// exists in an array or not

$array = array(
      'names' => array("Geek1", "Geek2", "Geek3"), 
    'rank' => array('1', '2', '3')
); 

// Use of array_key_exists() function
if(isset($array["rank"])){
    echo "Found the Key"; 
} 
else{ 
    echo "Key not Found"; 
} 

?> 

Output
Found the Key 

Method 3: Using the array_search function:

The array_search function is used to search for a given value in an array and return the corresponding key if the value is found. If the value is not found, it returns false.

Syntax:

bool = array_search($search_value, $array);
PHP
<?php
    $array = array('name' => 'geek', 'age' => '22');

if (array_search('geek', $array) !== false) {
    echo 'Found the key';
} else {
    echo 'Key not found';
}

?>

Output
Found the key

Method 4: Using array_keys

Using array_keys to check if a key exists involves getting all the keys of the array with array_keys and then using in_array to determine if the specific key is present

Example

PHP
<?php
$array = ['name' => 'John', 'age' => 30, 'city' => 'New York'];
$key = 'age';

if (in_array($key, array_keys($array))) {
    echo "Key '$key' exists in the array.";
} else {
    echo "Key '$key' does not exist in the array.";
}
?>

Output
Key 'age' exists in the array.

Using array_key_exists() with array_flip()

This approach involves flipping the array’s keys and values using array_flip(), then using array_key_exists() to check if the key exists in the flipped array.

Example:

PHP
<?php
function keyExistsUsingArrayFlip($array, $key) {
    $flippedArray = array_flip($array);
    return array_key_exists($key, $flippedArray);
}

// Example usage:
$arr = ["Geek1", "Geek2", "1", "2", "3"];
$key = "2";
if (keyExistsUsingArrayFlip($arr, $key)) {
    echo "Found the Key";
} else {
    echo "Key not Found";
}

?>

Output
Found the Key

Using in_array Function Directly

The in_array function checks if a specific value exists in an array, returning true if found and false otherwise. It’s used for value existence checks, not for keys.

Example

PHP
<?php

$arr = ["Geek1", "Geek2", "1", "2", "3"];
$key = "2";

if (in_array($key, $arr)) {
    echo "Found the Key";
} else {
    echo "Key not Found";
}

?>

Output
Found the Key




Previous Article
Next Article

Similar Reads

How to Check if a Key Exists in an Associative Array in PHP?
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. Table of Content Using array_key_exists()
3 min read
How to Check a Key Exists in JavaScript Object ?
Checking if a key exists in a JavaScript object involves verifying whether a specific property is defined within the object. This practice ensures data integrity, prevents errors, and facilitates smooth program execution by confirming property existence before accessing or manipulating it. Objects in JavaScript are non-primitive data types that hol
3 min read
JavaScript Check if a key exists inside a JSON object
In JavaScript, working with JSON (JavaScript Object Notation) objects is common, and often, you may need to check whether a certain key exists within an object. This article will tell various methods to accomplish this task efficiently. JSON objects, characterized by their key-value pairs, are versatile data structures used for storing and transmit
5 min read
How to Check if a Key Exists in a Dictionary in TypeScript ?
In TypeScript dictionaries are used whenever the data is needed to be stored in key and value form. We often retrieve the data from the dictionaries using an associated key. Therefore it becomes crucial to check whether the key exists in a dictionary or not. We can use the below methods to check if a key exists or not in a dictionary. Table of Cont
4 min read
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 is used to check the presence of an element in the
2 min read
How to Check if a Value Exists in an Associative Array in PHP?
Given an Associative array, the task is to check whether a value exists in the associative array or not. There are two methods to check if a value exists in an associative array, these are - using in_array(), and array_search() functions. Let's explore each method with detailed explanations and code examples. Table of Content Using in_array() Funct
2 min read
How to check if File Exists in PHP ?
To check whether any file is existing or not then we can use the below-mentioned PHP function. To find the existence of the files, we use file_exists() function. This function is used to check whether a file or directory exists or not. Syntax: file_exists( $path ) Parameters: This function accept only one parameter $path. It specifies the path of t
1 min read
How to make a key fire when key is pressed using JavaScript ?
In JavaScript, holding down a key will fire the key continuously until it is released. This behavior may be used in applications such as games where a key may be expected to fire only once even after being held down. This can be achieved using two methods: Method 1: Using a flag variable to check the current key status: A flag variable is defined w
3 min read
How to Merge Child Object Value to Parent Key Based on Key Name in JavaScript ?
JavaScript allows us to merge values from child objects into their parent object based on a specific key name. This can be useful for organizing data or simplifying complex structures. There are several approaches available in JavaScript to merge child object values to parent key based on the key name which are as follows. Table of Content Using fo
3 min read
How to check foreach Loop Key Value in PHP ?
In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such as Table of Content Using the for-each loop with simple valuesUsing the foreach loop with Key-Value pairsUsing array_keys Function to Access Keys and ValuesUsing the for-each loop with simple valuesWe can use the foreach loop to access and use t
3 min read
three90RightbarBannerImg