array_key_exists
Check whether there is a specified key name or index in the array
array_key_exists()
function checks whether the specified key name exists in an array. Returns true if the key name exists, and returns false if the key name does not exist.
Tip: Remember, if you omit the key name when specifying an array, an integer key name starting from 0 and each key value is incremented by 1. (See Example 2)
Check whether the key name "Volvo" exists in the array:
<?php $a = array ( "Volvo" => "XC90" , "BMW" => "X5" ) ; if ( array_key_exists ( "Volvo" , $a ) ) { echo "The key exists!" ; } else { echo "The key does not exist!" ; } ?>
Try it yourself
Check whether the key name "Toyota" exists in the array:
<?php $a = array ( "Volvo" => "XC90" , "BMW" => "X5" ) ; if ( key_exists ( "Toyota" , $a ) ) { echo "The key exists!" ; } else { echo "The key does not exist!" ; } ?>
Try it yourself
Check whether the integer key name "0" exists in the array:
<?php $a = array ( "Volvo" , "BMW" ) ; if ( array_key_exists ( 0 , $a ) ) { echo "The key exists!" ; } else { echo "The key does not exist!" ; } ?>
Try it yourself
array_key_exists ( key , array )
parameter | describe |
---|---|
key | Required. Specify key names. |
array | Required. Specify array. |