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

Modernizing Software Delivery in the Dutch Tech Landscape: A Pragmatic DevOps Playbook

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

Finding Qualified AWS DevOps Trainers: Enterprise Playbook

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

Teaching Failure: How to Build Incident Response and Resilience into DevOps Training

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 Open Road in Japan: An Independent Rider’s Handbook for Cross-Country Touring

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 Comprehensive Guide to Scooters, Motorcycles, and Road Trips

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

Monitoring and Observability Training: Complete Curriculum 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
guest

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x