Current Location: Home> Latest Articles> An efficient combination of array_column and in_array queries whether a value exists

An efficient combination of array_column and in_array queries whether a value exists

M66 2025-04-28

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.

1. Introduction to array_column function

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.

2. Introduction to in_array function

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).

3. Use array_column and in_array in combination

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:

  1. Use array_column to extract the name column.

  2. 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";
}

4. More complex queries

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";
}

5. Performance considerations

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.

Summarize

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.