Home >Backend Development >PHP Tutorial >php check if array subscript exists
PHP method to check whether array subscript exists
array_key_exists() function
array_key_exists () function determines whether the specified key exists in an array. If the key exists, it returns true, otherwise it returns false.
Syntax:
array_key_exists(key,array)
Parameter description:
key required. Specifies the key name.
array Required. Specifies the input array.
Instance:
<?php $a=array("a"=>"Dog","b"=>"Cat"); if (array_key_exists("a",$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } ?>
Output:
Key exists!
Instance:
<?php $a=array("a"=>"Dog","b"=>"Cat"); if (array_key_exists("c",$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } ?>
Output:
Key does not exist!
Instance:
<?php $a=array("Dog",Cat"); if (array_key_exists(0,$a)) { echo "Key exists!"; } else { echo "Key does not exist!"; } ?>
Output:
Key exists!
Recommended tutorial: PHP video tutorial
The above is the detailed content of php check if array subscript exists. For more information, please follow other related articles on the PHP Chinese website!