In PHP, the array_column and array_filter functions are very useful tools when processing array data, especially when it is necessary to extract specified column data from a two-dimensional array that meets certain criteria. This article will explain how to use these two functions to accomplish this.
The array_column function is used to extract the value of a column from a multidimensional array and return a new array. Its basic syntax is as follows:
array_column(array $array, $column_key, $index_key = null): array
$array : The input multi-dimensional array.
$column_key : The key name of the column you want to extract.
$index_key (optional): If this parameter is provided, the returned array will use this key as the key for the new array.
For example, suppose we have the following two-dimensional array:
$data = [
['id' => 1, 'name' => 'John', 'age' => 25],
['id' => 2, 'name' => 'Jane', 'age' => 30],
['id' => 3, 'name' => 'Joe', 'age' => 22],
];
We can extract the name column of all users through the array_column function:
$names = array_column($data, 'name');
print_r($names);
The output will be:
Array
(
[0] => John
[1] => Jane
[2] => Joe
)
The array_filter function is used to filter elements in an array and return elements that meet the criteria. Its basic syntax is as follows:
array_filter(array $array, ?callable $callback = null, int $mode = 0): array
$array : The input array.
$callback : Callback function, used to filter the conditions of an array.
$mode (optional): Filtering mode, ARRAY_FILTER_USE_KEY is used for key filtering, ARRAY_FILTER_USE_BOTH key and value filtering, default filtering.
Suppose we want to filter out users older than 24 years old from the previous array, and we can use array_filter with a callback function:
$filteredData = array_filter($data, function($person) {
return $person['age'] > 24;
});
print_r($filteredData);
The output will be:
Array
(
[1] => Array
(
[id] => 2
[name] => Jane
[age] => 30
)
[0] => Array
(
[id] => 1
[name] => John
[age] => 25
)
)
If you want to combine array_column and array_filter to extract specified column data that meets specific conditions, you can first use the array_filter function to filter the array, and then use the array_column function to extract the corresponding columns.
For example, suppose we need to find out the name columns of all users older than 24 years old:
$filteredNames = array_column(
array_filter($data, function($person) {
return $person['age'] > 24;
}),
'name'
);
print_r($filteredNames);
The output will be:
Array
(
[0] => John
[1] => Jane
)
This example shows how to get the data that meets the criteria by filtering the data ( array_filter ) first and then extracting the required column ( array_column ).
If your data source is a result returned by an API or website, the data returned is usually a JSON object containing multiple elements. For example, suppose we get user data from https://api.m66.net/users and we only care about user names that meet certain criteria (such as age older than 24). You can do it like this:
$url = "https://api.m66.net/users";
$response = file_get_contents($url);
$data = json_decode($response, true);
$filteredNames = array_column(
array_filter($data, function($person) {
return $person['age'] > 24;
}),
'name'
);
print_r($filteredNames);
In this example, firstly, the user data is obtained from m66.net through file_get_contents , then the array_filter is used to filter users older than 24 years old, and finally their name column is extracted through array_column .
With the combination of array_column and array_filter functions, you can easily extract specific column data that meets the criteria from complex two-dimensional arrays. This method is very useful for processing data returned by the API, database query results, etc., and can greatly simplify the complexity of data processing.
Related Tags:
array_filter