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

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.

Syntax
array_key_exists(key, array

Parameter Values

key - Required. Specifies the key

array - Required. Specifies an array

<?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";
}
?>

Output

The key 'France' is exists in the cities array

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...