Home  >  Article  >  Backend Development  >  How to detect whether a key exists in an array in php

How to detect whether a key exists in an array in php

青灯夜游
青灯夜游Original
2022-07-07 19:59:542299browse

Two detection methods: 1. Use array_key_exists() detection, the syntax "array_key_exists (specify key, specify array)", if the key exists, return true, if the key does not exist, return false. 2. Use the "$array name["specified key"]" statement to access the specified array element, and then use the isset() function to detect whether the array element exists. The syntax is "isset($array name["specified key"])". If Returns TRUE if the key exists, otherwise returns FALSE.

How to detect whether a key exists in an array in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

php checks whether the specified key name exists There are two methods for specifying an array:

  • Directly use the array_key_exists() function

  • Use the isset() function with "$Specify the array variable name ["Specify the key name"]" statement

Method 1: Use the array_key_exists() function

array_key_exists(specify key name, specify array) The function can detect whether the specified key name exists in an array. If the key name exists, it returns true. If the key name does not exist, it returns false.

"1","a"=>"",2=>"2","b"=>0,"c"=>"blue");
var_dump($arr);
if (array_key_exists("a", $arr)) {
	echo "指定键名'a' 存在于指定数组";
} else {
	echo "指定键名'a'不存在于指定数组";
}
?>

How to detect whether a key exists in an array in php

Method 2: Use the isset() function with the "array name["specified key name"]" statement

  • Use $ to specify the array variable name ["specified key name"] to access the specified array element,

  • Use isset( ) function detects whether the array element exists

    If it exists and is not NULL, it returns TRUE, otherwise it returns FALSE.

"1","a"=>"",2=>"2","b"=>0,"c"=>"blue");
var_dump($arr);
if (isset($arr["d"])) {
	echo "指定键名'd' 存在于指定数组";
} else {
	echo "指定键名'd'不存在于指定数组";
}
?>

How to detect whether a key exists in an array in php

Description:

  • ##array_key_exists( ) Function checks whether the specified key name exists in an array

Tip: Please remember that if you omit the key name when specifying the array, it will generate a value from 0 An integer key name that starts and increments by 1.

array_key_exists(key,array)

ParametersDescriptionRequired . Specifies the key name. Required. Specifies an array.
key
array
Return value: TRUE if the key name exists, FALSE if the key name does not exist.

  • isset() The function is used to detect whether the variable has been set and is not NULL.

If a variable has been released using unset(), it will return FALSE through isset().

If isset() is used to test a variable that is set to NULL, FALSE will be returned.

Also note that the null character ("\0") is not equivalent to PHP's NULL constant.

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to detect whether a key exists in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn