How to check a key exist in an array? explain with example

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

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 = [“Devop1”, “Devop2”, “1”, “2”,”3″]
key = “2”
Output: Found the Key

Input : arr = [“Devop1”, “Devop2”, “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 program to check if a key 
// exists in an array or not
  
$array = array(
      'names' => array("Devop1", "Devop2", "Devop3"), 
    '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 program to check if a key 
// exists in an array or not
  
$array = array(
      'names' => array("Devop1", "Devop2", "Devop3"), 
    '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

Related Posts

What will you learn from Laravel?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Error: MySQL shut down unexpectedly, How to solve this error on xampp?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

What is the requirement for learning Laravel?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

How to repair the corrupted table ‘global_priv’ in the localhost PHPMyAdmin database?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

Whenever you’re updating your PHP form data and getting a warning: count(): error message on the above page? How can you fix it?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More

If details values are not retrieving at your PHP form whenever you update/edit your form. How can you fix it?

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps! We spend hours scrolling social media and waste money on things we forget, but won’t spend 30…

Read More
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x