PHP array_key_exists() Function

What is PHP array_key_exists() Function?

If you want to know whether a specific key/index exists in an array, use key_exists() function to check it. The function doesn’t care about the types of the value or its existence.

Syntax:

array_key_exists(key, array)

Parameters:

The Function has 2 parameters; both are required-

key (Required): This is a key that the function searches within the array. It could be any data type – string / integer / float / bool / resource / null. Check example 1.

array (Required): This is an array into which the function searches the key/index.

Return Values:

The function returns-

  • TRUE – If it finds the key in the array.
  • FALSE – If it doesn’t find the key in the array.

How to use array_key_exists() function in coding?

In coding, sometimes we need to know whether a key exists in an array before we perform the other operation depending on the result. And you know, the function returns TRUE or FALSE depending on whether it finds the specified key in the array or not. So, usually, we use conditional statements (if statement, ternary operator) with this function like code below-

<?php
$colors = array("Red","Yellow", "Blue");
if(key_exists(0, $colors)){
    echo "Array key exists.";
}else{
    echo "Array key doesn't exist";
}
?>

Examples:

Example 1: Simple array_key_exists() function examples-

<?php
$student = ["James"=>35,"Robert"=>30,"David"=>33]; echo array_key_exists("James", $student)? "Key(James) found.":"Key(James) not found."; echo "<br />";
$numbers = [911,999,112]; echo array_key_exists(1, $numbers)? "Key(999) found.":"Key(999) not found."; echo "<br />";
$weight = [75.89=>"James",80.3=>"John",90.1=>"Abraham"]; echo array_key_exists(22.3, $weight)? "Key(90.1) found.":"Key(90.1) not found."; echo "<br />";
$attendance = [TRUE=>"Present",FALSE=>"Absent"]; echo array_key_exists(TRUE, $attendance)? "Key (Present) found.":"Key not found."; echo "<br />";
$height = [6.5=>"James",5.8=>"John",6=>"Abraham",NULL=>"Linda"]; echo array_key_exists(NULL, $height)? "Key(height) found.":"Key(height) not found"; echo "<br />";
$arr = ["PHP",8.32,TRUE]; echo array_key_exists(3, $arr)? "Key found.":"Key not found.";
?>

Output:

Key(James) found.
Key(999) found.
Key(90.1) not found.
Key (Present) found.
Key(height) found.
Key not found.

Explanation:

Line 2: the key_exists() uses an array that has string type keys.

Line 3: the key_exists() uses an array that has integer type keys.

Line 4: the key_exists() uses an array that has float type keys.

Line 5: the key_exists() uses an array that has Boolean type keys.

Line 6: the key_exists() uses an array that has NULL type key.

Example 2: Checking an array element existence before accessing it-

<?php
$colorModel = ["RGB","HSL","CMYK"];
if(array_key_exists(5, $colorModel)){
    echo "The value is: " . $colorModel[5];
    // Do further coding.
}else{
    echo "Error! Array key not fund.";
}
?>

Output:

Error! Array key not fund.

Explanation:

There are 3 keys in the array – 0, 1, 2. So, the key 5 doesn’t exist in the array. So, it is better to check whether the key exists in the array before doing further.

Example 3: Avoiding “Undefined array key” with array_key_exists()-

<?php
$bloodGroup = ["Group1"=>"A", "Group2"=>"B", "Group3"=>"O"];
echo $bloodGroup["Group4"];
if(array_key_exists("Group4", $bloodGroup)){
    echo "The value is: " . $bloodGroup["Group4"];
}
?>

Output:

Warning: Undefined array key “Group4” in D:\xampp\htdocs\array_key_exists function.php on line 3

Explanation:

Line 3: the key “Group4” doesn’t exist in the array $bloodGroup. So, the “Undefined array key” error occurs. So, you better check its existence at first like line no 4.

Example 4: array_key_exists() with indexed and associative array-

<?php
$nACountry = ["Canada", "USA"]; echo array_key_exists(1, $nACountry)? "Key(USA) found.":"Key(USA) not found."; echo "<br />";
$eUCountry = ["1st"=>"UK", "2nd"=>"Germany", "3rd"=>"France"]; echo array_key_exists("3rd", $eUCountry)? "Key(France) found.":"Key(France) not found.";
?>

Output:

Key(USA) found.
Key(France) found.

Explanation:

Line 2: $nACountry is an indexed array and array_key_exists() function can work on this keys.

Line 3: $eUCountry is an associative array and array_key_exists() function can work on this keys.

Practical Usages of array_key_exists() Function:

The function is useful when you want to make sure that an array contains a specific key before accessing its value to avoid errors. Check example 2.

Best Practices on array_key_exists() Function:

Before accessing an array key, use this function else you’ll get an “Undefined array key” error. Check example 3.

Notes on array_key_exists() Function:

  • The function can work on both indexed or associative array. Check example 4.
  • The function can differentiate uppercase and lowercase keys.
    <?php
    $country = ["Name"=> "France", "Language"=>"French", "language"=>"English", "Population"=>67750000, "Area"=>643,801];
    echo $country["Language"] . " - " .$country["language"];
    echo "<br />";
    if(!array_key_exists("LANGUAGE", $country)) echo "Key(LANGUAGE) not found.";
    ?>
    

    Output:

    French - English
    Key(LANGUAGE) not found.

    Explanation:

    The key_exists() function consider the keys “one” and “One” two different keys. For the same reason, the function doesn’t consider the key “ONE” exists in the array.

  • It is an alias of key_exists() function.

Caution:

  • The return value TRUE of this function doesn’t mean that the key contains a value that isset() function considers as TRUE. If an array value is NULL, the array_key_exists() function returns TRUE if you search for it but, the isset() function considers this variable as FALSE. Have a look at the example below-
    <?php
    $student = ["James", "Robert", NULL, "David", "Jordan"];
    echo array_key_exists(2, $student)? "Key(student) found.":"Key(student) not found."; echo "<br />";
    if(isset($student[2])) echo "Key(student) found."; else echo "Key(student) not found.";
    ?>
    

    Output:

    Key(student) found.
    Key(student) not found.

    Explanation:

    The array_key_exists() function returns TRUE if an element contains NULL value (Line 3). On the other hand, the isset() function considers the element doesn’t have any value. So, it returns FALSE.

  • The function doesn’t return TRUE even though the key exists in the array if the key resides inside the inner array. That means, the function doesn’t consider keys of the inner array of multidimensional array. Check the example below-
    <?php
    $students = ["studentNo"=>2,
            "student1"=>["name"=>"James", "age"=>35, "height"=>6.5],
            "student1"=>["name"=>"Robert", "age"=>30, "height"=>6.2]
            ];
    echo array_key_exists("studentNo", $students)? "Key(studentNo) found.":"Key(studentNo) not found."; echo "<br />";
    echo array_key_exists("name", $students)? "Key(studentNo) found.":"Key(name) not found."; echo "<br />";
    ?>
    

    Output:

    Key(studentNo) found.
    Key(name) not found.

    Explanation:

    The key “studentNo” exists in the array, but the function returns “Key(name) doesn’t exist.” In the second line because the key(name) is in the inner array and the array_key_exists() function only works in outer most array, not in inner array.

  • As this function rounds the float key down to integer, you may get a different result. Look the example below-
    <?php
    $fraction = [1=>"One", 10=>"Ten", 1.5 => "One point five", 8.2 => "eight point two",];
    var_dump("Value of the key 1 is: " . $fraction[1]);
    ?>
    

    Output:

    string(37) "Value of the key 1 is: One point five"

    Explanation:

    The key 1.5 becomes 1 and then, there are two keys named “1”. And, in this case the later key is taken. So, the output is unexpected.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP array_key_exists() Function

You’ll often use this built-in array function. Use array_key_exists() function when you just want to when whether a specific key exists in an array regardless of its value. You don’t care about the type of the value.

Reference:

https://www.php.net/manual/en/function.array-key-exists