PHP: array_key_exists() function
PHP: Checks if the given key or index exists in an array
The array_key_exists() function is used to check whether a specified key is present in an array or not.
The function returns TRUE if the given key is set in the array. The key can be any value possible for an array index.
Version:
(PHP 4 and above)
Syntax:
array_key_exists(array_key, array_name)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
array_key | Value to check. | Required | Mixed* |
array_name | The specified array whose keys will be checked. | Required | Array |
*Mixed: Mixed indicates that a parameter may accept multiple (but not necessarily all) types.
Return value
TRUE on success or FALSE on failure.
Value Type: Boolean
Example:
<?php
$array1=array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400);
if (array_key_exists("Banana",$array1))
{
echo "Array Key exists...";
}
else
{
echo "Array Key does not exist...";
}
?>
Output:
Array Key exists.
Pictorial Presentation:

View the example in the browser
Practice here online :
See also
Previous:array_intersect
Next: array_keys
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises