array_key_exists(): searching for key

This function Searches for presence of a input key.
bool = array_key_exists ($key_to_search, $input_array);
$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.";}
Output
Name field exists.

Example: Handling Multidimensional Arrays

$data = [
    'user' => ['name' => 'Alice', 'age' => 25],
    'order' => ['id' => 123, 'total' => 50]
];
if (array_key_exists('name', $data['user'])) {
    echo "Name exists in user array.";
}
Output
Name exists in user array.

Example: Comparing array_key_exists() and isset()

$arr = ['name' => null];
echo isset($arr['name']);  // Output: false
echo '<br>';
echo array_key_exists('name', $arr);  // Output: true
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.
FunctionDetails
in_array()Search for value inside Array. Returns TRUE or FALSE
array_search()Search for value inside Array. Returns the key if found, FALSE otherwise
array_keys()Array of keys for matching values, FALSE otherwise
Array REFERENCE
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer