In determining whether the index value of a PHP array exists, generally use isset and array_key_exists two methods, but the values returned by the two will be different, let me introduce isset and array_key_exists differences, the need for friends to refer to.
Let's start with a simple example.
The code is as follows |
Copy Code |
Isset ($a [' key ']) Array_key_exists (' key ', $a) array_key_exists |
To tell you exactly whether a key exists for an array, and isset simply returns the state of whether the key value is null.
The Isset function is to detect whether a variable is set.
Format: bool Isset (mixed var [, mixed Var [, ...])
return value:
1. Returns FALSE if the variable does not exist
2. Returns FALSE if the variable exists and its value is null
3. Returns TURE if the variable exists and the value is not NULL
4. When checking multiple variables at the same time, returns TRUE if each item meets the previous requirement, otherwise the result is FALSE
Example 1
The code is as follows |
Copy Code |
$a = Array (' key1 ' = ' 123 ', ' key2 ' = null); |
Using these two methods to determine the existence of key values, the results are as follows:
The code is as follows |
Copy Code |
Isset ($a [' key1 ']); True Array_key_exists (' Key1 ', $a); True Isset ($a [' key2 ']); False Array_key_exists (' Key2 ', $a); True |
Example 2
The code is as follows |
Copy Code |
$a = array (' test ' = = 1, ' hello ' = NULL); Var_dump (isset ($a [' test '));//TRUE Var_dump (isset ($a [' foo '));//FALSE Var_dump (isset ($a [' hello '));//FALSE ' Hello ' equals NULL, so it is considered to be unassigned. If you want to detect NULL key values, you can try the methods below. Var_dump (array_key_exists (' hello ', $a)); TRUE ?> |
http://www.bkjia.com/PHPjc/628922.html www.bkjia.com true http://www.bkjia.com/PHPjc/628922.html techarticle in determining whether the index value of a PHP array exists, generally use isset and array_key_exists two methods, but the values returned by the two are different, let me introduce Isset and array ...