PHP array_key_exists

 The PHP array_key_exists function (function) can be used to check whether the specified key or index exists in the PHP Array array . The PHP array_key_exists function is sometimes compared with the PHP isset structure function, regardless of the structure relationship of the PHP isset , The execution speed will be faster than PHP array_key_exists, but the array_key_exists function is quite intuitive after all, and many PHP designers will use it, and under certain conditions, the return results of array_key_exists and isset are still different.


PHP array_key_exists basic syntax

bool array_key_exists (the key name $key to be queried, the PHP array to be checked)


The first parameter in parentheses is "the key name $key to be queried" is a required item, which can be any value (value) or an array index (array index), the second parameter is "the PHP to be checked "Array" is also a required item. If the key or index to be queried is found in the array, the PHP array_key_exists function will return true, if not found, it will return false.

PHP array_key_exists reference example
<?php
$NewArray = array('A' => '1','B' => 2);
if (array_key_exists('A', $NewArray)) {
 echo "$NewArray array contains key name A";
}
?>

At the beginning of the example, a new array $NewArray is prepared. There are two key names, A and B. Then we use the PHP array_key_exists function to find out whether the key name A is in $NewArray. If so, echo The output text "$NewArray contains key name A". Please note: The array_key_exists function can only be used to check whether there is a specified key name in the array, there is no way to check the key value.

Post a Comment

0 Comments