PHP detects whether an array has a key value to determine whether an array index exists array_key_exists ();
BOOL Array_key_exists (mixed key, array search)
The code is as follows |
Copy Code |
<?php /*
Array_key_exists () returns TRUE if the given key exists in the array. The key can be any value that can be indexed as an array.
*/
$search _array = Array ("The" => 1, "second" => 4); if (array_key_exists ("$search _array)") { echo "The ' The ' '" ' "The '" ' "is in the array"; } ?> |
There's empty,isset on the Internet. You can also verify that the key value exists
Empty: When the parameter is 0 or null (as in the above example), empty returns True, see empty official manual for details.
Isset: When the parameter is NULL, the return of false,0 and null is different in PHP, isset (0) returns True
The code is as follows |
Copy Code |
<?php $a = Array (' A ' =>1, ' B ' =>0, ' C ' =>null);
Echo ' A test by empty: ', empty ($a [' a '])? ' Not exist ': ' exist ', php_eol; Echo ' A test by isset: ', isset ($a [' a '])? ' exist ': ' Not exist ', php_eol; Echo ' A test by array_key_exists: ', array_key_exists (' a ', $a)? ' exist ': ' Not exist ', php_eol, Php_eol;
Echo ' B Test by empty: ', empty ($a [' B '])? ' Not exist ': ' exist ', php_eol; Echo ' B Test by isset: ', isset ($a [' B '])? ' exist ': ' Not exist ', php_eol; Echo ' B Test by array_key_exists: ', array_key_exists (' B ', $a)? ' exist ': ' Not exist ', php_eol, Php_eol;
Echo ' C Test by empty: ', empty ($a [' C '])? ' Not exist ': ' exist ', php_eol; Echo ' C Test by Isset: ', isset ($a [' C '])? ' exist ': ' Not exist ', php_eol; Echo ' C Test by array_key_exists: ', array_key_exists (' C ', $a)? ' exist ': ' Not exist ', php_eol, Php_eol; The output results are as follows: ======================================================== A test by empty:exist A test by isset:exist A test by array_key_exists:exist b Test by Empty:not exist B Test by Isset:exist B Test by Array_key_exists:exist C Test by Empty:not exist C Test by Isset:not exist C Test by Array_key_exists:exist |
three different ways of grammatical differences
Empty: When the parameter is 0 or null (as in the above example), empty returns True, see empty official manual for details.
Isset: When the parameter is NULL, the return of false,0 and null is different in PHP, isset (0) returns True
Array_key_exists: Purely to determine whether an array key value exists, regardless of the value
So, from the point of view of accuracy, array_key_exists is the most accurate!
performance comparisons in three different ways
Get a set of data from the Internet, see here or resources, as follows:
For a small array:
Array_key_exists:float (0.18357992172241)
Empty:float (0.072798013687134)
Isset:float (0.070242881774902)
For a relative big array:
Array_key_exists:float (0.57489585876465)
Empty:float (0.0068421363830566)
Isset:float (0.0069410800933838)
You can see that in the case of large data, empty and isset performance than array_key_exists faster than the 2 order of magnitude, the difference is still very large. If you frequently judge, you still need to optimize. The reason for such a large performance difference, individual speculation, may be that isset and empty as PHP syntax structure is not a function, PHP interpreter did optimization, and array_key_exists as a function, there is no correlation optimization. Specific reasons, to be studied through the source code.
three ways to use recommendations
(Given that empty is similar to isset performance, but isset accuracy is higher, only isset and array_key_exists are compared here)
If the array is unlikely to have a null value, it is recommended that you use Isset
If the value is often null in the array, it is recommended that you use Array_key_exists
If the value in the array may appear to be null, but less often, it is recommended to combine isset with array_key_exists, such as "if" Isset ($arr [' key ']) | | array_key_exists (' key ', $arr)) {/* Do somthing*/} ". This approach takes care of both performance and accuracy, but the code gets longer.