Q:

How to check if a key exists in an array in PHP

0

How to check if a key exists in an array in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the PHP array_key_exists() function

You can use the PHP array_key_exists() function to test whether a given key or index exists in an array or not. This function returns TRUE on success or FALSE on failure.

Let's take a look at the following example to understand how it actually works:

<?php
$cities = array("France"=>"Paris", "India"=>"Mumbai", "UK"=>"London", "USA"=>"New York");
 
// Testing the key exists in the array or not
if(array_key_exists("France", $cities)){
    echo "The key 'France' is exists in the cities array";
}
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to remove the last element from an array in PH... >>
<< How to reverse the order of an array in PHP...