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)
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

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    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