Home >Backend Development >PHP Problem >How to query specified elements in an array in php
In PHP, array is a very commonly used data type. When operating an array, sometimes we need to query whether there is a specified element in the array. At this time, we can use some functions provided in PHP to achieve this.
1. in_array() function
PHP provides the in_array() function, which is used to search for a specified value in an array. Returns TRUE if the value is found, FALSE otherwise.
The syntax of the in_array() function is as follows:
in_array($needle, $haystack, $strict)
Among them, $needle represents the element to be found, and $haystack represents The array to query, $strict indicates whether to use strict mode.
Strict mode means that data types must also match, while non-strict mode only matches values. Therefore, in array queries, we usually use non-strict mode.
For example, we have an array $a, containing the elements "apple", "banana", and "orange". Now we want to query whether the element "banana" exists. The code is as follows:
$a = array("apple", "banana", "orange");
if (in_array("banana", $a)) {
echo "存在";
} else {
echo "不存在";
}
The execution result is: exists.
2. array_search() function
Similar to the in_array() function, PHP also provides the array_search() function. The difference is that the array_search() function returns the key name of the query element in the array, rather than TRUE or FALSE.
The syntax of the array_search() function is as follows:
array_search($needle, $haystack, $strict)
Among them, $needle represents the element to be found, and $haystack represents The array to query, $strict indicates whether to use strict mode. If the query element does not exist in the array, returns FALSE.
For example, we have an array $b, containing key names "1", "2", "3" and element values "apple", "banana", "orange", now we want to query whether it exists Element "orange". The code is as follows:
$b = array("1"=>"apple", "2"=>"banana", "3"=>"orange");
if (($key = array_search("orange", $b)) !== false) {
echo "存在,键名为:" . $key;
} else {
echo "不存在";
}
The execution result is : Exists, key name: 3.
3. array_key_exists() function
If we only need to query whether a specified key exists in an array, and do not care about its corresponding element value, we can use array_key_exists() provided in PHP )function.
The syntax of the array_key_exists() function is as follows:
array_key_exists($key, $array)
Among them, $key represents the key name to be found, and $array represents the query array.
For example, we have an array $c, containing key names "1", "2", "3" and element values "apple", "banana", "orange", now we want to query whether it exists Key name "2". The code is as follows:
$c = array("1"=>"apple", "2"=>"banana", "3"=>"orange");
if (array_key_exists("2", $c)) {
echo "存在";
} else {
echo "不存在";
}
The execution result is: exists.
4. isset() function
In addition, you can also use the isset() function in PHP to query whether the specified element exists in the array. The isset() function determines whether the variable has been defined and is not empty.
isset() function’s syntax is as follows:
isset($array[$key])
Among them, $array represents the array to be queried, and $key represents the array to be searched. Elements.
For example, we have an array $d, containing the elements "apple", "banana", and "orange". Now we want to query whether the element "banana" exists. The code is as follows:
$d = array("apple", "banana", "orange");
if (isset($d[1])) {
echo "存在";
} else {
echo "不存在";
}
The execution result is: exists.
To sum up, there are many ways to query specified elements in an array in PHP. When using it, we need to choose the appropriate method based on specific business needs and usage scenarios.
The above is the detailed content of How to query specified elements in an array in php. For more information, please follow other related articles on the PHP Chinese website!