Home > Article > Backend Development > PHP array learning how to check whether there is a specified key name/value
In the previous article "PHP array learning: How to traverse array elements? In "A Brief Analysis of 4 Methods", we introduced 4 methods of traversing arrays. Today we will take a look at the key names and key values of array elements, and introduce the method of checking whether the specified key name/value exists in a PHP array.
First let’s take a look at How to detect whether the key name is located in the array? In fact, it is very simple. We can use the built-in function array_key_exists() to detect it.
The array_key_exists() function can check whether the specified key name (or index) exists in an array; this function accepts two parameters $key and $array, which are used to specify the key name and array respectively. Returns true if the key name $key exists in the array $array, and returns false if it does not exist.
Let’s take a closer look through code examples.
<?php header("Content-type:text/html;charset=utf-8"); $array= array("姓名"=>"张三","年龄"=>25,"性别"=>"男"); var_dump($array); $key = '年龄'; if( array_key_exists($key, $array) ){ //检测数组中是否存在该键 echo "键名 '$key' 存在于数组中!"; } ?>
Output result:
Next let’s take a lookMain difference between isset() and array_key_exists() functions:
## The #array_key_exists() function will explicitly tell whether the key exists in the array, while isset() will only return true if the key/variable exists and is not null. Also, isset() does not render an error when the array/variable does not exist, while array_key_exists() does.
How to detect whether the key value is located in the array? This is also simple. We can use the built-in function in_array() to detect. If a given value is found in a given array, it will return TRUE, otherwise it will return FALSE.
in_array($value,$array,$type)You can check whether the specified value exists in the specified array
$array$value
; The
$type parameter can be omitted.
<?php header("Content-type:text/html;charset=utf-8"); $array= array("姓名"=>"张三","年龄"=>25,"性别"=>"男"); var_dump($array); $value = '张三'; if( in_array($value, $array) ){ //检测数组中是否存在该键名 echo "键名 '$value' 存在于数组中!"; } ?>Output result: The third parameter of the in_array() function
$type is omitted, but if set If the value is true, it will be checked whether the type of the searched data and the value of the array are the same. At this point, the function returns true only if the element exists in the array and has the same data type as the given value.
<?php header("Content-type:text/html;charset=utf-8"); $array= array("姓名"=>"张三","年龄"=>25,"性别"=>"男"); var_dump($array); $value = "25"; if( in_array($value, $array,true) ){ //检测数组中是否存在该键名 echo "键名 '$value' 存在于数组中!"; }else{ echo "键名 '$value' 不存在于数组中!"; } echo "<br>"; $value = 25; if( in_array($value, $array,true) ){ //检测数组中是否存在该键名 echo "键名 $value 存在于数组中!"; }else{ echo "键名 '$value' 不存在于数组中!"; } ?>Output result: And if the
$value parameter is a string, and the
$type parameter Set to true to make the search case-sensitive.
Recommended: 《PHP interview questions summary (collection)》
The above is the detailed content of PHP array learning how to check whether there is a specified key name/value. For more information, please follow other related articles on the PHP Chinese website!