In PHP, it is a common operation to determine whether an array contains a specified value. Sometimes we need to search an array to determine whether a certain value exists, or we need to judge whether a specified key name exists in the array, etc. This article will introduce common methods and application scenarios in PHP to determine whether an array has a specified value.
The in_array() function is one of the functions in PHP used to determine whether a specified value exists in an array. Its syntax is:
in_array($needle, $haystack, $strict)
This function accepts three parameters:
$needle
: the value to be found $haystack
: to be found in Array of values$strict
: Optional parameter, if this parameter is set to true
, a type comparison will be performedThe following is a sample code that uses the in_array() function to determine whether a specified value exists in an array:
$fruits = array("apple", "banana", "orange"); if (in_array("banana", $fruits)) { echo "存在"; } else { echo "不存在"; }
In the above example, we first defined an array containing the names of three fruits$fruits
, and then use the in_array() function to check whether the element "banana"
exists. The output of this example is exists
.
array_search()
function is also one of the functions used to find array elements in PHP. This function returns a key whose value corresponds to the specified search value. If the value is not found, the function returns false
. Its syntax is as follows:
array_search($needle, $haystack, $strict)
The function accepts three parameters:
$needle
: The value to find$ haystack
: Array in which to look for values $strict
: Optional parameter, if this parameter is set to true
, a type comparison will be performed The following is an example code that uses the array_search() function to determine whether a specified value exists in an array:
$fruits = array("apple", "banana", "orange"); $index = array_search("banana", $fruits); if ($index !== false) { echo "存在,位置为:".$index; } else { echo "不存在"; }
In the above example, we also defined a file containing three types of fruit names. The array $fruits
, and then use the array_search()
function to find the element "banana"
and get the corresponding key name. If found, the output exists at position: 1
.
It should be noted that the most significant difference between the array_search()
function and the in_array()
function is that the former returns the key name of the element, while the latter only returns the key name of the element. Returns true or false.
array_key_exists()
The function is used to check whether the specified key exists in the array. The syntax of this function is as follows:
array_key_exists($key, $array)
This function accepts two parameters:
$key
: The key name to be found$array
: The array in which to find the key nameThe following is a sample code that uses the array_key_exists()
function to determine whether the specified key name exists in the array:
$fruits = array("apple" => 1, "banana" => 2, "orange" => 3); if (array_key_exists("banana", $fruits)) { echo "存在"; } else { echo "不存在"; }
In the above example, we defined an associative array containing three key-value pairs. Use the array_key_exists()
function to find the key name "banana"
. The output of this example is exists
.
isset()
function is used to check whether the variable is set and not null
. By using the isset()
function in an array, we can check whether a key exists in the array. The syntax is as follows:
isset($array[$key])
It should be noted that the isset()
function can only be used to check whether a variable is set, but cannot check whether the variable contains a value.
The following is an example code that uses the isset()
function to determine whether the specified key name exists in the array:
$fruits = array("apple" => 1, "banana" => 2, "orange" => 3); if (isset($fruits["banana"])) { echo "存在"; } else { echo "不存在"; }
In the above example, we also defined an association Array, use the isset()
function to find the key name "banana"
. The output of this example is exists
.
The above are some common ways to determine whether an array contains a specified value in PHP. According to actual needs, we can choose to use the in_array()
, array_search()
, array_key_exists()
or isset()
function to implement Different search operations. When using these functions, we need to understand the syntax and differences of different functions in order to use them more flexibly.
The above is the detailed content of PHP determines whether an array has a specified value. For more information, please follow other related articles on the PHP Chinese website!