In PHP, array_column and in_array are two commonly used functions, which are used to extract a column in an array and to check whether a value exists in an array. Using these two functions in combination can effectively query whether a value in a multidimensional array exists.
The array_column function is used to extract the value of a column from a multidimensional array and return a new array containing the values of that column. The basic usage of this function is as follows:
array_column($array, $column_key, $index_key);
$array : The input multi-dimensional array.
$column_key : The column name or index of the column you want to extract.
$index_key (optional): If you want the returned array to be keyed with an index, you can pass this parameter.
The in_array function is used to check whether a value exists in an array. It returns true if the value exists, otherwise it returns false . Its usage is as follows:
in_array($needle, $haystack, $strict);
$needle : The value to be found.
$haystack : The array being searched.
$strict (optional): If set to true , in_array will perform strict comparisons (i.e., check not only if the value exists, but also if the data type matches).
We use array_column and in_array to efficiently find out whether a value exists in a specific column in a multidimensional array. For example, suppose we have an array of multiple user information and we want to check if a username exists.
Suppose there is the following array:
$users = [
['id' => 1, 'name' => 'Tom', 'email' => 'tom@m66.net'],
['id' => 2, 'name' => 'Jerry', 'email' => 'jerry@m66.net'],
['id' => 3, 'name' => 'Spike', 'email' => 'spike@m66.net'],
];
We want to check if the username Jerry exists in the array. You can follow the following steps:
Use array_column to extract the name column.
Use in_array to check if the username exists in the extracted array.
$names = array_column($users, 'name'); // Extract all user names
$user_exists = in_array('Jerry', $names); // examine 'Jerry' Does it exist
if ($user_exists) {
echo "user Jerry exist";
} else {
echo "user Jerry 不exist";
}
Sometimes, we don't just query a simple value, we also need to check based on multiple conditions. By combining array_column and in_array , we can find specific values in specific columns. This method is very efficient when processing large data sets, avoiding the tedious operation of manually traversing multi-dimensional arrays.
For example, if we want to check if a certain email address exists:
$emails = array_column($users, 'email'); // 提取所有user的Mail
$email_exists = in_array('jerry@m66.net', $emails); // examineMailDoes it exist
if ($email_exists) {
echo "Mail jerry@m66.net exist";
} else {
echo "Mail jerry@m66.net 不exist";
}
In PHP, array_column and in_array are relatively efficient functions, especially when dealing with larger arrays. Directly using in_array to search for multi-dimensional arrays will traverse the entire array, which is less efficient. By first using array_column to extract the target column and then using in_array for searching, unnecessary comparison operations can be reduced and performance can be improved.
Using array_column and in_array in combination is a very efficient way, especially suitable for finding whether a value exists in a multidimensional array. By first extracting the required columns and then using in_array to check whether the target value exists, you can improve query efficiency and reduce the complexity of your code. This approach can significantly improve performance when processing larger data sets.