Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

array_key_exists()

Example

Verify if the key “Volvo” is present in an array.

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

Definition and Usage

The array_key_exists() function checks if a specified key is present in an array, returning true if the key exists and false if it does not.

 

Tip: If you do not specify a key when defining an array, integer keys are automatically generated, starting from 0 and incrementing by 1 for each value.

Syntax

array_key_exists(key, array)

Parameter Values

Parameter

Description

key

Required. Specifies the key to check for.

array

Required. Specifies the array to search.

Technical Details

 

Return Value:

Returns TRUE if the key exists and FALSE if it does not.

PHP Version:

4.0.7+

More Examples

Example

 

Check if the key “Toyota” is present in an array.

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

Example

Check if the integer key “0” exists in an array.

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