Determining whether an element is in an array is a very common operation in PHP. Array is a very important data structure, and using array operations in PHP is very simple, so many developers use arrays to store and process data when writing PHP programs. Determining whether an element is in an array is also a commonly used operation. So how should we determine whether an element is in an array in PHP? The following will introduce in detail how to determine whether it is in an array in PHP.
1. Use the in_array() function
The in_array() function is a function in PHP used to determine whether an element is in an array. The syntax format is as follows:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
In this function, $needle represents the element that needs to be judged, $ haystack represents the array that needs to be judged, and $strict is used to specify whether strict type judgment is required. The function return value is a Boolean type, indicating whether $needle exists in the array $haystack.
The following is an example of using the in_array() function to determine whether an element is in an array:
$fruits = array("apple", "banana", "orange");
if (in_array("banana", $fruits)) {
echo "存在";
} else {
echo "不存在";
}
In this example, we define a $fruits array , and use the in_array() function to determine whether the "banana" element exists in the array $fruits. Since "banana" is an element in the $fruits array, the program prints "exists".
2. Use the array_search() function
The array_search() function is a function in PHP used to find an element in an array. The syntax format is as follows:
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
In this function, $needle represents the element to be found, $ haystack represents the array that needs to be searched, and $strict is used to specify whether strict type judgment is required. The return value of the function is the key name of the element in the array (that is, the array subscript). If the element is not found, false is returned.
The following is an example of using the array_search() function to determine whether an element is in an array:
$fruits = array("apple", "banana", "orange");
$key = array_search("banana", $fruits);
if ($key !== false) {
echo "存在,索引为:".$key;
} else {
echo "不存在";
}
In this example, we use the array_search() function to find the position of the "banana" element in the $fruits array, and assign its return value to the variable $key. If $key is not false, it means that the "banana" element exists in the array and its index is output. Otherwise, it means that the "banana" element does not exist in the array, and the program will output "does not exist".
3. Use in_array() function and array_keys() function
In addition to using in_array() function and array_search() function to determine whether an element is in the array, we can also use in_array in combination () function and array_keys() function to make judgments. The array_keys() function is a function in PHP used to obtain all key names (i.e. array subscripts) in an array. The syntax format is as follows:
array array_keys (array $array [, mixed $search_value = null [, bool $strict = false ]] )
In this function, $array represents the need to obtain An array of key names, $search_value is used to specify whether a specified element needs to be searched, and $strict is used to specify whether strict type judgment is required. This function returns an array containing all the keys of the array, arranged in the order of the elements in the array.
The following is an example of using the in_array() function and array_keys() function to determine whether an element is in an array:
$fruits = array("apple", "banana", " orange");
$keys = array_keys($fruits, "banana");
if (in_array("banana", $fruits)) {
echo "存在,索引为:".$keys[0];
} else {
echo "不存在";
}
In this example, we first use the array_keys() function to get all the key names with the value "banana" in the $fruits array, and assign the return value to the variable $keys. Then use the in_array() function to determine whether the "banana" element exists in the array. If it exists, output its index value $keys[0] (note that since $keys is an array, you need to use $keys[0] to Get the value of its first element). If it does not exist, output "does not exist".
4. Use foreach loop
In addition to using the above functions, we can also use foreach loop to determine whether an element is in the array. This method can be applied to any type of array, not just ordinary indexed arrays.
The following is an example of using a foreach loop to determine whether an element is in an array:
$fruits = array("apple", "banana", "orange");
$exist = false;
foreach ($fruits as $fruit) {
if ($fruit == "banana") { $exist = true; break; }
}
if ($exist) {
echo "存在";
} else {
echo "不存在";
}
In this example, we use a foreach loop to traverse the $fruits array, and each time we determine whether the current element $fruit is equal to "banana". If so, set the $exist variable to true and break out of the loop; otherwise, continue the loop. Finally, determine whether the "banana" element exists in the array based on the value of $exist.
In this article, we introduce 4 methods in PHP to determine whether an element is in an array, including the in_array() function, array_search() function, in_array() function and array_keys() function, and the use of foreach loop . In actual programming, we need to choose the most suitable method to determine whether an element is in an array based on specific needs, and we must pay attention to the type judgment problem to avoid errors.
The above is the detailed content of How to determine whether it is in an array in php. For more information, please follow other related articles on the PHP Chinese website!