array_key_exists()
18th January 2021
0 Comments
Programming Language
PHP [Versions: 4 >= 4.0.7, 5, 7, 8]
Syntax
Typical Usage
Checks if a key or index exists in a PHP array.
Accepted Parameters
- key (string or integer) (Required): The key or index to look for.
- array (array) (Required): The array you wish to check.
Return Value(s)
Returns true on success or false on failure.Notes
array_key_exists() will only search the first dimension of multidimensional arrays.
Examples of array_key_exists()
Example 1
<?php
$cats = array('lions' => 5, 'tigers' => 4, 'leopards' => '8');
if (array_key_exists('tigers', $cats)) {
echo 'This zoo has ' . $cats['tigers'] . ' tigers.';
}
The above will output “This zoo has 4 tigers.” because the $cats array does have a ‘tigers’ key.
Used In Projects
Was This Helpful?
If this post has helped you, please let me know. Your feedback will be highly appreciated and will help me build better content.