PHP array_key_exists() function

P
PHP array_intersect_ukey() function
PHP array_keys() function

The array_key_exists function checks if the specified key exists in the array. If the key exists, it returns true, false in case of the key is not found.

What is the syntax of the array_key_exists function in PHP?

array_key_exists(key, array)

Parameter Values

ParametersDetails
keyRequired. Specifies the key
arrayRequired. Specifies an array
array_key_exists function in PHP

Example of array_key_exists function

<?php
$a=array("Honda"=>"XC90","BMW"=>"X5");
if (array_key_exists("Honda",$a))
  {
  echo "Key exists!";
  }
else
  {
  echo "Key does not exist!";
  }
?>

In the above example, the array_key_exists checks for the “Honda” key in the array.

<?php
$a=array("Volvo","BMW");
if (array_key_exists(0,$a))
  {
  echo "Key exists!";
  }
else
  {
  echo "Key does not exist!";
  }
?>

In the above example, the name (string) of the key is not specified in the function. In this case, when no string is specified to the array_key_exists function, it assumes the integer key that starts from 0 and increments for every key in the specified array.

PHP array_intersect_ukey() function
PHP array_keys() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z