This article describes how to use php to determine whether a specified key exists in an array. The example shows how to use array_key_exists and isset in php, which is very useful, for more information about how to use php to determine whether a specified key exists in an array, see the following example. Share it with you for your reference. The specific analysis is as follows:
Php has two functions to determine whether the array contains the specified keys: array_key_exists and isset.
The array_key_exists syntax is as follows:
array_key_exists($key, $array)
If the key exists, the true isset function syntax is returned as follows:
isset($array[$key])
Returns true if the key exists.
The demo code is as follows:
<?php$array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");print("Is 'One' defined? ".array_key_exists("One", $array)."\n");print("Is '1' defined? ".array_key_exists("1", $array)."\n");print("Is 'Two' defined? ".isset($array["Two"])."\n");print("Is '2' defined? ".isset($array[2])."\n");?>
The returned results are as follows:
Is 'One' defined? 1Is '1′ defined?Is 'Two' defined? 1Is '2′ defined?
I hope this article will help you with php programming.