Home > Article > Backend Development > Detailed explanation of the usage of array_key_exists() function in PHP function library
In the PHP function library, array_key_exists() is a very commonly used function. Its function is to determine whether the specified key name exists in the array. In many scenarios, we need to determine whether an array contains a specific key name and perform corresponding operations if it exists. At this time, the role of the array_key_exists() function is reflected.
The array_key_exists() function has two parameters. The first parameter is the key name to be found, and the second parameter is the array name. This function returns a Boolean value, true if the specified key is found, otherwise false.
Below we use several examples to explain the usage of the array_key_exists() function in detail.
Example 1: Determine whether the specified key exists in the array
In the following example, we define an array $arr, and then use the array_key_exists() function to check the array $key Whether it exists in the array $arr. If the array $key exists, output "exists", otherwise output "does not exist".
$arr = array("name"=>"张三","age"=>18,"gender"=>"男"); $key = "name"; if(array_key_exists($key,$arr)){ echo "存在"; }else{ echo "不存在"; }
The above code will output "exists" because the key name "name" does exist in the array $arr.
Example 2: Determine whether multiple key names exist in the array at the same time
If we need to find out whether multiple key names exist in the array at the same time, we can also use the array_key_exists() function, Just call the function multiple times.
In the following example, we simultaneously search whether three key names in the array $arr exist. If all three key names exist, output "all exist", otherwise output "not all exist".
$arr = array("name"=>"张三","age"=>18,"gender"=>"男"); $key1 = "name"; $key2 = "age"; $key3 = "class"; if(array_key_exists($key1,$arr) && array_key_exists($key2,$arr) && array_key_exists($key3,$arr)){ echo "全部存在"; }else{ echo "不都存在"; }
The above code will output "Not all exist" because the key name "class" does not exist in the array $arr.
Example 3: Find whether a key name exists in multiple arrays
Sometimes we need to find whether the same key name exists in multiple arrays. At this time, we can first use a foreach loop to traverse multiple arrays, and use the array_key_exists() function to check whether the key name exists.
In the following example, we define two arrays $arr1 and $arr2, and then use a foreach loop to traverse the two arrays to find whether the key name "name" exists in these two arrays.
$arr1 = array("name"=>"张三","age"=>18,"gender"=>"男"); $arr2 = array("name"=>"李四","age"=>20,"gender"=>"女"); $key = "name"; foreach(array($arr1,$arr2) as $arr){ if(array_key_exists($key,$arr)){ echo "存在"; }else{ echo "不存在"; } }
The above code will output "exists" and "exists" because the key name "name" exists in the arrays $arr1 and $arr2.
Summary:
The above is a detailed explanation of the usage of the array_key_exists() function. Generally speaking, it is very convenient to use this function to check whether the specified key name exists in the array. Especially when dealing with large arrays, this function can greatly simplify the search process. Therefore, it is very necessary for PHP developers to learn and master the usage of this function.
The above is the detailed content of Detailed explanation of the usage of array_key_exists() function in PHP function library. For more information, please follow other related articles on the PHP Chinese website!