Home >Backend Development >PHP Problem >How to determine if a value is in an array in php
PHP, as a scripting language widely used in web development, has strong data processing capabilities, among which arrays are one of the important data types in PHP. In PHP, we often need to determine whether a value exists in an array, so determining whether a value in PHP is in an array is a very common problem. This article will explore this issue in depth.
1. Introduction to PHP arrays
In PHP, arrays are a commonly used data type with the following characteristics:
For example, the following is a simple PHP array:
$cities=array("Beijing","Shanghai","Guangzhou","Shenzhen");
In this array, $cities
is an array variable name, array( "Beijing","Shanghai","Guangzhou","Shenzhen")
is an array composed of four elements. Each element corresponds to a city name of string type, and the subscripts are 0, 1, and 2, 3.
2. How to determine whether a value is in an array in PHP
Now suppose we have an array $arr
and a value $value
, We need to determine whether this value is in the array. There are several ways to accomplish this in PHP, including using the in_array()
function, using the array_search()
function, using isset()
and array_key_exists()
functions, etc., will be introduced separately below.
in_array()
function to determine whether the value is in the arrayin_array()
The function is used to check a value exists in an array and returns a Boolean value. Returns True
if $value
exists in $arr
, otherwise returns False
.
in_array($value,$arr);
Here is an example:
$fruits=array("apple","banana","orange","kiwi"); if(in_array("banana",$fruits)){ echo "查找到了值!"; }else{ echo "未查找到值."; }
This code will output The value was found!
.
array_search()
function to determine whether the value is in the arrayarray_search()
The function is used to try to search in the array Finds an element in and returns its key (i.e. subscript). If $value
exists in $arr
, returns the key of this element, otherwise returns False
.
array_search($value,$arr);
Here is an example:
$fruits=array("apple","banana","orange","kiwi"); $index=array_search("banana",$fruits); echo $index;
This code will output 1 because banana
corresponds to the second element in the $fruits
array , its subscript is 1.
It should be noted that if $value
corresponds to multiple elements, only the subscript of the first element encountered will be returned.
isset()
and array_key_exists()
functions to determine whether the value is in the array##isset( ) function is used to detect whether the variable is set. If the variable exists and the value is not
NULL, it returns
True, otherwise it returns
False.
isset($arr[$key]);
array_key_exists() Function is used to check whether the specified key or index exists in the array. If
$key exists in
$arr, returns
True, otherwise returns
False.
array_key_exists($key,$arr);The following is an example:
$fruits=array("apple"=>1,"banana"=>2,"orange"=>3,"kiwi"=>4); if(isset($fruits["banana"])){ echo "查找到了值!"; }else{ echo "未查找到值."; }This code will output
The value was found!, because the key value in the array is
banana Elements.
in_array() function and the
array_search() function are more common methods. They are also relatively efficient methods.
in_array() The function returns a Boolean value, there is no need to query the position of the element in the array, and it is faster. The
array_search() function returns the index of the element in the array, which is slightly slower.
isset() function and the
array_key_exists() function is more commonly used for associative arrays (that is, arrays whose subscripts are strings). The difference between these two functions is that the
isset() function returns
True when the target value exists, and still returns
False when the target value does not exist; and
array_key_exists() The function returns
False when the target value does not exist, and
True only when the target value exists.
in_array() function, using the
array_search() function and using the
isset() and
array_key_exists() functions. These methods are relatively simple in implementation and can be selected and used according to needs. At the same time, other methods or algorithms can also be selected according to the actual situation. Either way, the key is a trade-off between correctness and efficiency.
The above is the detailed content of How to determine if a value is in an array in php. For more information, please follow other related articles on the PHP Chinese website!