ItSolutionStuff.com

How to Check Key Exists in Array in PHP?

By Hardik Savani • May 14, 2024
PHP

Hey Developer,

Hello, all! In this article, we will talk about check if key exists in array php. I explained simply step by step check if key present in array php. you'll learn php check if array key is set. you'll learn php check if array key is defined.

we will use array_key_exists() function and isset() function to check key is exists in php array. so, let's see the simple code of how to check key exists in array in php.

Example 1:

index.php

<?php

$array = ['id' => 1, 'name' => 'Hardik Savani', 'email' => 'hardik@gmail.com'];

if (array_key_exists('name', $array)) {

echo('The key is exists.');

} else {

echo('The key is not exists.');

}

?>

Output:

The key is exists.

Example 2:

index.php

<?php

$array = ['id' => 1, 'name' => 'Hardik Savani', 'email' => 'hardik@gmail.com'];

if (isset($array['name'])) {

echo('The key is exists.');

} else {

echo('The key is not exists.');

}

?>

Output:

The key is exists.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP array_merge() Function Example

Read Now →

How to Merge Two Arrays with Unique Values in PHP?

Read Now →

How to Merge Two Array in PHP?

Read Now →

How to Remove String Keys in PHP Array?

Read Now →

How to Remove Numeric Keys in PHP Array?

Read Now →

How to Remove String Values in PHP Array?

Read Now →

How to Remove Numeric Values in PHP Array?

Read Now →

How to Get First 10 Elements of Array in PHP?

Read Now →

How to Get First 5 Elements of Array in PHP?

Read Now →

How to Get Last 5 Elements of Array in PHP?

Read Now →

How to Get First Element of Array in PHP?

Read Now →

Laravel Array Length Validation Example

Read Now →

PHP Remove Element from Array by Value Example

Read Now →