micheledurante/array-aggregate
Composer 安装命令:
composer require micheledurante/array-aggregate
包简介
A PHP, SQL-like stream aggregate function to group a set of rows by any number of columns. You can also define how to compute groups
README 文档
README
A PHP, SQL-like stream aggregate function to group a set of rows by any number of columns. You can also define how to compute groups. Rows are expected to be already sorted on the columns used to aggregate results.
function array_aggregate(array $columns, array $rows, callable $compute_func = null): array
The order in which columns are given to the function does not affect the output. Columns must comparable with the ===
operator.
Inspired by Craig Freedman's SQL articles on MS dev blog https://blogs.msdn.microsoft.com/craigfr/2006/09/13/stream-aggregate.
Installation
As a composer package:
composer require micheledurante/array-aggregate
Or require the source file in src/array_aggregate.php.
Usage
$rows = [ 0 => [ 'store_id' => 1, 'manager_id' => 2, 'name' => 'Alice' ], 1 => [ 'store_id' => 2, 'manager_id' => 3, 'name' => 'Bob' ], 2 => [ 'store_id' => 2, 'manager_id' => 3, 'name' => 'Eve' ], 3 => [ 'store_id' => 2, 'manager_id' => 4, 'name' => 'Foobar' ] ]; $groups = array_aggregate(array('store_id', 'manager_id'), $rows, function (array $group): array { return [ 'store_id' => $group[0]['store_id'], 'manager_id' => $group[0]['manager_id'], 'people' => implode(',', array_column($group, 'name')) ]; }); var_export($groups);
Results in:
/* array ( 0 => array ( 'store_id' => 1, 'manager_id' => 2, 'people' => 'Alice' ), 1 => array ( 'store_id' => 2, 'manager_id' => 3, 'people' => 'Bob,Eve' ), 2 => array ( 'store_id' => 2, 'manager_id' => 4, 'people' => 'Foobar' ) ) */
Similar Projects
array_group_by()
By jakezatecky. Main difference is that
array_aggregate() won't change the structure of the array by creating new keys/nested groups. It behaves like the SQL
GROUP BY clause, which returns the aggregates of the input rows. Both functions can work with multiple keys.
array_aggregate() doesn't allow a callable to match columns.
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2019-06-16