PHP array_key_exists()

PHP array_key_exists() Function

PHP array_key_exists() is an inbuilt function in PHP and is used to check whether a specific key or index exists in the array or not. This function return boolean type value “True” or “False”. If specified key is found in the array, return the true otherwise false. To learn more about PHP Array Functions.

PHP array_key_exists() Syntax

boolean array_key_exists ($key, $array)

$key: This argument refers to the key that you want to search in given array.
$array: This argument refers to the array in which you want to search the given key “$key”.

Return Value: This function will return the boolean type value “True” or “False”. It depends, key is exist in the array or not.

PHP Associative Array

<?php
$arr = array("naresh" => 15, "ankur" => 18);
$key = "naresh";

if (array_key_exists($key,$arr))
  {
  echo "Wow ! Key exists in Array";
  }
else
  {
  echo "Key does not exist!";
  }
?>

Output

Wow ! Key exists in Array

Example 2

<?php
$arr = array("james" => 15, "david" => 18);
$key = "smith";

if (array_key_exists($key,$arr))
  {
  echo "Wow ! Key exists in Array";
  }
else
  {
  echo "Key does not exist!";
  }
?>

Output

Key does not exist!

Indexed Array PHP

<?php
$arr = array("david","james","john","smith");
$key = 1;

if (array_key_exists($key,$arr))
  {
  echo "Wow ! Key exists in Array";
  }
else
  {
  echo "Key does not exist!";
  }
?>

Output

Wow ! Key exists in Array

Example 2

<?php
$arr = array("david","james","john","smith");
$key = 4;

if (array_key_exists($key,$arr))
  {
  echo "Wow ! Key exists in Array";
  }
else
  {
  echo "Key does not exist!";
  }
?>

Output

Key does not exist!

More Related Post

Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below.

Leave a comment

Your email address will not be published. Required fields are marked *

0 0 0
FOLLOW ON