bool = array_key_exists ($key_to_search, $input_array);
$key_to_search : Required , key to search inside the $input_array. $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
$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
$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
$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)
$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.
$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.
$arr = ['name' => null];
echo isset($arr['name']); // Output: false
echo '<br>';
echo array_key_exists('name', $arr); // Output: true
Explanation:| Function | Details |
|---|---|
| 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 |
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.