How to check a key exist in 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 can check a key exist or not in an array with the help of array_key_exist() function in PHP. array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist,  if we skip the key when we specify an array, an integer key is generated which starts at 0 and increases by 1 for each value.

syntax

array_key_exists(key, array)

this function takes two argument and this function returns boolean values(TRUE/FALSE), nested keys always return false value.

example1

<?php

        // with key values
		$shivam = array("shivam" => "23","ravi" => "24","abhi" => "25");
		
			if (array_key_exists("abhi",$shivam))
			{
				echo "Key exists";
			}
			else
			{
				echo "Key not exist";
			}
?> 

output

Key exists

example 2

<?php
       // with no key values
		$shivam = array("shivam","ravi","abhi");
		
			if (array_key_exists(0,$shivam))
			{
				echo "Key exists";
			}
			else
			{
				echo "Key not exist";
			}
?> 

output

Key exists

Related Posts

Mastering the DevOps Salary Landscape: A Comprehensive Guide

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

The Complete DevOps Certification Guide: From Beginner to Expert Architect

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

A Guide to Selecting the Right DevOps Training Format

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

DevOps Certification Training: Identifying Quality and Expertise

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

Complete DevOps Training Guide for Assessing Mentor Technical Competence

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

Mastering Your Future: The Complete Guide to the Canada PR CRS Calculator

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