Home > Article > Backend Development > How to check if data is in an array in php
In PHP, sometimes you need to query whether a piece of data is in an array. This is a problem that is often encountered in development. This article will introduce this problem in detail, including various query methods and their usage scenarios.
Method 1: in_array() function
The in_array() function is a built-in function in PHP, used to check whether a value is in an array. This function can accept two parameters, the first parameter is the value to be found, and the second parameter is the array to query. This function returns a Boolean value, if the query result is true, it returns true, if the query result is false, it returns false.
The sample code is as follows:
$array = array('apple', 'banana', 'orange'); if (in_array('apple', $array)) { echo "apple is in the array"; } else { echo "apple is not in the array"; }
In the above example, we define an array, and then use the in_array() function to query whether 'apple' is in the array, and the final output result is " apple is in the array".
Method 2: array_search() function
The array_search() function is also a built-in function in PHP, used to query the position of a value in an array. This function can accept two parameters, the first parameter is the value to be found, and the second parameter is the array to query. If the query result is true, the index of the value in the array is returned; if the query result is false, false is returned.
The sample code is as follows:
$array = array('apple', 'banana', 'orange'); $key = array_search('apple', $array); if ($key !== false) { echo "apple is in the array at index " . $key; } else { echo "apple is not in the array"; }
In the above example, we also define an array, and then use the array_search() function to query the index of 'apple' in the array, and finally output the result is "apple is in the array at index 0".
It should be noted that the query result of the array_search() function may be 0, so you need to use the loose equality operator "!== false" to determine whether the query result is false.
Method 3: in_array() function and strict equality operator
We can also combine the in_array() function with the strict equality operator "===" to ensure the correctness of the query results . The strict equality operator differentiates between types, thus avoiding query errors caused by type mismatches.
The sample code is as follows:
$array = array('1', 2, '3.14', 'apple'); if (in_array(2, $array, true)) { echo "2 is in the array"; } else { echo "2 is not in the array"; }
In the above example, we define an array containing an integer 2, and use the in_array() function and the strict equality operator to query whether 2 is in the In the array, the final output result is "2 is in the array".
It should be noted that if the strict equality operator is not used, the value 2 and the string '2' will be considered equal, so incorrect query results may be returned.
Method 4: in_array() function and array_map() function
We can also use the in_array() function combined with the array_map() function to query whether multiple values are in the array. The array_map() function can apply a user-defined function to each element in an array and return a new array composed of the processing results.
The sample code is as follows:
$array = array('apple', 'banana', 'orange'); $search = array('apple', 'pear', 'grape'); $result = array_map(function ($val) use ($array) { return in_array($val, $array); }, $search); print_r($result);
In the above example, we define an array and an array of values to be queried, and then use the array_map() function to map the array of values to be queried. Apply the in_array() function to each element in and return a new array composed of the query results. The final output result is:
Array ( [0] => 1 [1] => 0 [2] => 0 )
In the result, 1 indicates that the query result is true, and 0 indicates that the query result is false.
It should be noted that using the array_map() function to query whether an array contains multiple values in another array will return a new array, so you need to use functions such as print_r() to output the query results .
To sum up, there are many methods to choose from in PHP to query whether a data is in an array. The appropriate method needs to be selected based on specific usage scenarios and needs.
The above is the detailed content of How to check if data is in an array in php. For more information, please follow other related articles on the PHP Chinese website!