Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/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
https://github.com/micheledurante/array-aggregate
array-function php stream-aggregate
Last synced: about 1 month ago
JSON representation
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
- Host: GitHub
- URL: https://github.com/micheledurante/array-aggregate
- Owner: micheledurante
- License: gpl-3.0
- Created: 2019-05-27T18:39:13.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-07-05T08:54:26.000Z (over 1 year ago)
- Last Synced: 2024-04-21T07:20:19.156Z (9 months ago)
- Topics: array-function, php, stream-aggregate
- Language: PHP
- Homepage:
- Size: 43 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# array_aggregate
[![Build Status](https://travis-ci.org/micheledurante/array-aggregate.svg?branch=master)](https://travis-ci.org/micheledurante/array-aggregate)
[![Latest Stable Version](https://img.shields.io/packagist/php-v/micheledurante/array-aggregate.svg)](https://packagist.org/packages/micheledurante/array-aggregate)
[![Latest Stable Version](https://img.shields.io/packagist/v/micheledurante/array-aggregate.svg)](https://packagist.org/packages/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. Rows are expected to be already sorted on the columns used to aggregate results.```php
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
```php
$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:
```php
/*
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](https://github.com/jakezatecky/array_group_by). 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.