As developers, we often need to process and operate arrays. In PHP, determining whether a certain value exists in an array is a frequently occurring task.
In PHP, there are many ways to check whether an array contains a certain value. This article will introduce several commonly used methods.
1. in_array() function
in_array() is one of PHP’s built-in functions, used to check whether a value exists in an array. Its syntax is as follows:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
This function has three parameters, which are the values to be found (needle ), the array to be found (haystack), and an optional bool type parameter strict. Among them, needle and haystack must be passed in, and the default value of strict is false.
If the value needle to be found is in the array haystack to be found, return true; otherwise, return false. Here is an example:
$fruits = array("apple", "banana", "orange");
if (in_array("apple", $fruits)) {
echo "找到了 apple!";
}
In the above example, $fruits is an array containing several fruit names, and the in_array() function will check whether it contains the string "apple". Because the array does contain "apple", the output is "Apple found!".
2. array_search() function
array_search() function is similar to in_array(), and is also used to find whether a value is in an array. The difference is that it returns the found key name (key) instead of returning a bool type result. The syntax is as follows:
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
This function has three parameters, where needle is the value to be found, haystack is the array to be searched, and the strict parameter is whether to enable strict comparison of types.
If the value needle to be found exists in the array haystack to be found, return the key name corresponding to the value; otherwise, return false. Here is an example:
$casual_fruits = array("apple", "banana", "orange");
$key = array_search("banana", $casual_fruits);
if ( $key) {
echo "banana 在数组中的键名是 " . $key;
}
In the above example, the $casual_fruits array contains several exotic fruit names, and the array_search() function finds whether it contains the value "banana" , returns "1" after finding it, and outputs "banana's key name in the array is 1" through echo.
3. The difference between in_array() and array_search() functions
Although the in_array() and array_search() functions are both used to find whether an array contains a certain value, their return values are different. The applicable scenarios are also different.
in_array() returns a bool type value, which is usually used to determine whether a value exists without knowing its position in the array. Array_search() returns a key name, which is usually used to check whether a value exists and needs to know its position in the array.
If you are only interested in the existence of the result, use the in_array() function; if you need to obtain the position of the value in the array, use the array_search() function.
4. Array_key_exists() function
In addition to checking whether a value exists in the array, we sometimes also need to check whether a certain key name exists. At this time, you need to use the PHP built-in function array_key_exists(). The syntax is as follows:
bool array_key_exists (mixed $key, array $array)
This function has two parameters, where the key parameter is the key name to be found, and the array parameter is the key to be found. array.
If the key name exists in the array, return true, otherwise return false. Here is an example:
$arr = array("a" => "apple", "b" => "banana");
if (array_key_exists("a", $arr )) {
echo "键名 a 存在于该数组中。";
}
In the above example code, we created an array containing several key-value pairs. We check whether the key name "a" exists, and because it does exist, we output "The key name a exists in this array.".
5. isset() function
Different from the array_key_exists() function, the isset() function can not only check whether a key name exists, but also check whether the value corresponding to the key name exists. The syntax is as follows:
bool isset ( mixed $var [, mixed $... ] )
This function can accept multiple parameters, each of which can be a variable or Is the key name of an array. Returns true if the parameter exists and its value is not NULL, false otherwise.
Here is an example:
$arr = array("a" => "apple", "b" => "banana");
if (isset( $arr["a"])) {
echo "键名 a 存在于该数组中,并且它的值是 " . $arr["a"];
}
In the above example code, we checked the "a" key name using isset() function, if the key name exists , then output the value corresponding to the key name.
Summary
To check whether an array contains a certain value in PHP, we can use the in_array(), array_search(), array_key_exists() and isset() functions. Among them, in_array() and array_search() are mainly used to check whether the value exists, while array_key_exists() and isset() functions are mainly used to check whether the key name exists.
If we only need to check whether a value exists in an array, use the in_array() function. If we need to get the key name of a value in an array, we can use array_search(). If we need to determine whether a key name exists in the array, use the array_key_exists() function; if we need to determine whether a key name and its corresponding value exist at the same time, we can use the isset() function.
The above is the detailed content of php detects whether there is an array. For more information, please follow other related articles on the PHP Chinese website!