In PHP, processing and filtering two-dimensional arrays is a common task, especially when the data volume is large. Fortunately, PHP provides several very useful built-in functions that can help us do this in an efficient way. In this article, we will focus on the array_filter() and array_column() functions and show how they can be combined to quickly process and filter data in two-dimensional arrays.
The array_column() function is used to extract the value of a column from a multidimensional array. It is great for extracting the values of a specific column from a 2D array. The basic syntax of this function is as follows:
array_column(array $array, mixed $column_key, mixed $index_key = null): array
$array : a two-dimensional array.
$column_key : The key of the column to be extracted.
$index_key : Optional parameter, specifying the index of the returned array.
For example, we have a two-dimensional array where each subarray represents the information of a user:
$users = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net'],
];
We want to extract the name column of all users, we can use array_column() :
$names = array_column($users, 'name');
print_r($names);
The output will be:
Array
(
[0] => Alice
[1] => Bob
[2] => Charlie
)
The array_filter() function is used to filter elements in an array and return elements that meet the specified conditions. The basic syntax is as follows:
array_filter(array $array, callable $callback, int $mode = 0): array
$array : The input array.
$callback : Callback function, used to judge each element of the array.
$mode : optional parameter that determines how the callback function works, usually using the default value 0 .
Suppose we want to filter out users whose email address contains m66.net . You can use array_filter() to filter users that meet the criteria.
$filteredUsers = array_filter($users, function($user) {
return strpos($user['email'], 'm66.net') !== false;
});
print_r($filteredUsers);
The output will be:
Array
(
[0] => Array
(
[id] => 1
[name] => Alice
[email] => alice@m66.net
)
[1] => Array
(
[id] => 2
[name] => Bob
[email] => bob@m66.net
)
[2] => Array
(
[id] => 3
[name] => Charlie
[email] => charlie@m66.net
)
)
Sometimes we need to extract specific columns at the same time and filter them. For example, we want to filter out the email addresses of all m66.net domain names and extract only the names of these users. We can use array_filter() and array_column() in combination.
$filteredEmails = array_filter($users, function($user) {
return strpos($user['email'], 'm66.net') !== false;
});
$names = array_column($filteredEmails, 'name');
print_r($names);
The output will be:
Array
(
[0] => Alice
[1] => Bob
[2] => Charlie
)
Suppose we have a list of user data obtained from the API, and each user's information contains an email address. We want to filter out all users using the m66.net domain name and extract the user's name and email address from it.
First, we get the data through the API and store it in the $users array:
$users = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@m66.net'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@m66.net'],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@m66.net'],
['id' => 4, 'name' => 'David', 'email' => 'david@example.com'],
['id' => 5, 'name' => 'Eve', 'email' => 'eve@m66.net'],
];
Next, use array_filter() to filter out users whose email contains m66.net , and use array_column() to extract the name and email:
$filteredUsers = array_filter($users, function($user) {
return strpos($user['email'], 'm66.net') !== false;
});
$names = array_column($filteredUsers, 'name');
$emails = array_column($filteredUsers, 'email');
print_r($names);
print_r($emails);
The output will be:
Array
(
[0] => Alice
[1] => Bob
[2] => Charlie
[3] => Eve
)
Array
(
[0] => alice@m66.net
[1] => bob@m66.net
[2] => charlie@m66.net
[3] => eve@m66.net
)
In this way, we not only filter out the users who meet the criteria, but also extract their names and email addresses.
The above is the method to quickly and efficiently process and filter data in a two-dimensional array through array_filter() and array_column() . These two functions can greatly improve the readability and performance of your code, especially when dealing with large data sets. Hope you can use them flexibly in actual projects!