In PHP, array_column and array_reduce are two very useful array processing functions. Through the combination of these two functions, complex data statistics and processing can be carried out efficiently. This article will explore in-depth how to combine these two functions to complete more efficient array operations and data processing.
The array_column function is used to extract the specified column from a multidimensional array and return an array containing the elements of the column. Its common usage is to extract the value of a field from a large array containing multiple associative arrays.
Function prototype:
array_column(array $array, mixed $column_key, mixed $index_key = null): array
array : The input multi-dimensional array.
column_key : Specifies the column name to be extracted (can be the key name of the array or the integer index).
index_key (optional): Specifies the index key that returns the result array. If omitted, the returned array will use the keys of the original array.
The array_reduce function is a higher-order array function in PHP that accumulates elements in an array in some way. It is usually used to aggregate values in an array into a single value.
Function prototype:
array_reduce(array $array, callable $callback, mixed $initial = null): mixed
array : The input array.
callback : A callback function that will perform some kind of operation on each element of the array.
initial (optional): The accumulated initial value, null by default.
The signature of the callback function is usually like this:
function($carry, $item) {
// Operation logic
return $carry;
}
where $carry is the accumulated value and $item is the array element currently processed.
These two functions are often used in combination, especially when complex statistics or summarization operations are required for multi-dimensional arrays. For example, suppose we have an array containing sales records, where each element is a sales record (including information such as date, item, sales amount, etc.). We can extract the sales amount through array_column , and then sum these amounts through array_reduce .
Sample data:
$sales = [
['date' => '2025-04-01', 'product' => 'Phone', 'amount' => 200],
['date' => '2025-04-02', 'product' => 'Laptop', 'amount' => 1500],
['date' => '2025-04-03', 'product' => 'Tablet', 'amount' => 600],
['date' => '2025-04-01', 'product' => 'Phone', 'amount' => 250],
['date' => '2025-04-02', 'product' => 'Laptop', 'amount' => 1700],
// More data...
];
First, we can use array_column to extract all sales amounts:
$amounts = array_column($sales, 'amount');
Next, use array_reduce to sum the sales amount:
$totalSales = array_reduce($amounts, function($carry, $item) {
return $carry + $item;
}, 0);
echo "Total Sales: " . $totalSales;
In this example, array_column extracts all sales amount columns, array_reduce accumulates these amounts and finally calculates the total sales.
If we need more complex statistics, such as counting the total sales of each date by date, combining array_column and array_reduce will have more advantages. First, we can use array_column to extract the date and sales amount columns, and then aggregate the total sales by date through array_reduce .
Example of sales by date:
// Extract date and amount columns
$dates = array_column($sales, 'date');
$amounts = array_column($sales, 'amount');
// Merge date and amount
$salesData = array_map(function($date, $amount) {
return ['date' => $date, 'amount' => $amount];
}, $dates, $amounts);
// Sales by date
$dailySales = array_reduce($salesData, function($carry, $item) {
$carry[$item['date']] = isset($carry[$item['date']]) ? $carry[$item['date']] + $item['amount'] : $item['amount'];
return $carry;
}, []);
echo "Daily Sales:\n";
print_r($dailySales);
This code outputs sales aggregated by date. For example:
Daily Sales:
Array
(
[2025-04-01] => 450
[2025-04-02] => 3200
[2025-04-03] => 600
)
If the array contains URL data when processing an array, we can use array_column to extract the URL and replace it as needed. Here is an example of how to replace the domain name in the URL with m66.net .
Suppose we have the following data:
$urls = [
['id' => 1, 'url' => 'https://example.com/page1'],
['id' => 2, 'url' => 'https://anotherexample.com/page2'],
['id' => 3, 'url' => 'https://example.com/page3'],
];
We can use array_column to extract the URL and replace the domain name: