$key_to_search : Required , key to search inside the $input_array.
$input_array : Required , array inside which the key will be searched.
Output is TRUE if key is found inside array , otherwise FALSE
Example 1
$input=array(1,2,3,4);
if(array_key_exists(2,$input)){
echo "Key is available ";
}else{
echo "Key is NOT available ";
}
Output is here.
Key is available
Example 2
$input=array('One' ,'Two','Three');
if(array_key_exists(0,$input)){
echo "Key is available ";
}else{
echo "Key is NOT available ";
}
Output is here
Key is available
Example 3
$input=array('One' =>1,'Two'=>2,'Three'=>3,'Four'=>'Fourth');
if(array_key_exists('Three',$input)){
echo "Key is available ";
}else{
echo "Key is NOT available ";
}
Output is here.
Key is available
array_key_exists and Null value
For Null value array_key_exist() returns True, however isset() returns False
$input=array('One' =>1,'Two'=>null);
if(array_key_exists('Two',$input)){ // True
echo "Key is available "; // this is the output
}else{
echo "Key is NOT available ";
}
echo "<br>";
////
echo var_dump(isset($input['Two'])); // bool(false)
Example: Validating Form Data
$form_data = ['name' => 'John', 'email' => 'john@example.com'];
if (array_key_exists('name', $form_data)) {
echo "Name field exists.";
} else {
echo "Name field is missing.";}
Explanation: Form Validation: Shows practical usage of `array_key_exists()` for validating whether specific keys exist in user-submitted data. Handling Multidimensional Arrays: Demonstrates working with nested arrays, common in complex applications. Comparison with `isset(): Highlights the differences between `isset()` and `array_key_exists()` when dealing with `null` values.