PHPackages                             jenner/array\_group\_by - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jenner/array\_group\_by

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jenner/array\_group\_by
=======================

like mysql group by

v1.3.1(9y ago)438.0k17MITPHPPHP &gt;=5.3.0

Since Jan 21Pushed 9y ago5 watchersCompare

[ Source](https://github.com/huyanping/Zebra-PHP-ArrayGroupBy)[ Packagist](https://packagist.org/packages/jenner/array_group_by)[ RSS](/packages/jenner-array-group-by/feed)WikiDiscussions master Synced today

READMEChangelog (4)DependenciesVersions (5)Used By (0)

Zebar-PHP-ArrayGroupBy
======================

[](#zebar-php-arraygroupby)

[![Build Status](https://camo.githubusercontent.com/d8c86f352f62799cd678a95d84d5ad622e9b60688a61ca3dc3246b1158d613eb/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f687579616e70696e672f5a656272612d5048502d417272617947726f757042792f6d61737465722e7376673f7374796c653d666c6174)](https://travis-ci.org/huyanping/Zebra-PHP-ArrayGroupBy)[![Latest Stable Version](https://camo.githubusercontent.com/ca8a98e86e8a600b3f900ae4bf41d6fb82b8ac1411193385fe249f23845dd9ef/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a656e6e65722f61727261795f67726f75705f62792e7376673f7374796c653d666c6174)](https://packagist.org/packages/jenner/array_group_by)[![Total Downloads](https://camo.githubusercontent.com/a7b50651a54eec02b14cf39db14ac83ffb27bbbcd727579bbc52b42b47b424fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a656e6e65722f61727261795f67726f75705f62792e7376673f7374796c653d666c6174)](https://packagist.org/packages/jenner/array_group_by)[![License](https://camo.githubusercontent.com/a5da832fcfeded62145746cfaf293fd1a74b31d6059d75340387d431ff7fc030/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a656e6e65722f61727261795f67726f75705f62792e7376673f7374796c653d666c6174)](https://packagist.org/packages/jenner/array_group_by)为什么使用Zebra-PHP-ArrayGroupBy
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[](#为什么使用zebra-php-arraygroupby)

在如下场景中，我们总是希望能够在php中使用类似mysql的groupby操作：

- SQL过于复杂，造成数据库运算效率低下
- 从数据库中读取出原始数据，在php中进行运算，增强代码重用率
- 其他非数据库场景的数组归并场景

Zebar-PHP-ArrayGroupBy能够做什么
---------------------------

[](#zebar-php-arraygroupby能够做什么)

- 对二维数组进行归并
- 归并的同时，支持对字段进行自定义处理
- 对数据组按照某一字段排序
- 比SQL更灵活的自定义函数，你可以随意编写归并和字段合并函数

示例：

```
$records = [
    ['order_date' => '2014-01-01', 'price' => 5],
    ['order_date' => '2014-01-02', 'price' => 10],
    ['order_date' => '2014-01-03', 'price' => 20],
    ['order_date' => '2015-01-04', 'price' => 25],
];

$group_by_fields = [
    'order_date' => function($value){
            return date('Y', strtotime($value));
        }
];

$group_by_value = [
    'order_date' => [
        'callback' => function($value_array){
                return substr($value_array[0], 0, 4);
            },
        'as' => 'year'
    ],
    'price' => function($data){
            return array_sum(array_column($data, 'price'));
        },
];

$grouped = \Jenner\Zebra\ArrayGroupBy::groupBy($records, $group_by_fields, $group_by_value);
print_r($grouped);
```

结果：

```
Array
(
    [0] => Array
        (
            [year] => 2014
            [price] => 35
        )

    [1] => Array
        (
            [year] => 2015
            [price] => 25
        )

)
```

举例

- 归并过程中，实现对结果的中值计算
- 归并过程中，对时间字段进行自定义处理，例如归并每5分钟的数据
- 等等

链式调用
----

[](#链式调用)

```
$records = [
    ['bill_time'=>'2014-01-01 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-01 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-01 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-02 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-02 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-03 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-03 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-03 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-04 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-04 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-04 00:00:00', 'price'=>1, 'cnt'=>3,],
    ['bill_time'=>'2014-01-04 00:00:00', 'price'=>1, 'cnt'=>3,],
];

$group_by_fields = [
    'bill_time' => function($field){
            return substr($field, 0, 10);
        }
];

$group_by_values = [
    'bill_time' => function($field_values){
            return substr($field_values[0], 0, 10);
        },
    'price' => function($field_values){
            return array_sum($field_values);
        },
    'cnt' => function($field_values){
            return array_sum($field_values);
        }
];

$week_fields = [
    'bill_time' => function($field){
            return date('w', strtotime($field));
        }
];

$week_values = [
    'bill_time' => function($data){
            return date('w', strtotime($data[0]['bill_time']));
        },
    'price' => function($data){
            return array_sum(array_column($data, 'price'));
        },
    'cnt' => function($data){
            return array_sum(array_column($data, 'cnt'));
        }
];

$grouped = (new \Jenner\Zebra\ArrayGroupBy($records))
            ->groupByField($group_by_fields)->groupByValue($group_by_values)
            ->groupByField($week_fields)->groupByValue($week_values)->get();
print_r($grouped);

// order by 示例
$order_result = (new \Jenner\Zebra\ArrayGroupBy($records))
            ->groupByField($group_by_fields)
            ->groupByValue($group_by_values)->orderBy('bill_time', SORT_DESC, 'price', SORT_ASC);
```

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~244 days

Total

4

Last Release

3448d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d132b55d12cd603f8cce9c152999e2b3f98748d56fe8a21adf62d5830d88d87?d=identicon)[huyanping](/maintainers/huyanping)

---

Top Contributors

[![white-poto](https://avatars.githubusercontent.com/u/4362540?v=4)](https://github.com/white-poto "white-poto (35 commits)")

---

Tags

arraygroup by

### Embed Badge

![Health badge](/badges/jenner-array-group-by/health.svg)

```
[![Health](https://phpackages.com/badges/jenner-array-group-by/health.svg)](https://phpackages.com/packages/jenner-array-group-by)
```

###  Alternatives

[doctrine/collections

PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.

6.0k430.2M1.4k](/packages/doctrine-collections)[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k317.3M3.2k](/packages/symfony-property-access)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k430.4M1.7k](/packages/nette-utils)[league/config

Define configuration arrays with strict schemas and access values with dot notation

565335.0M36](/packages/league-config)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k13.2M174](/packages/cuyz-valinor)[aimeos/map

Easy and elegant handling of PHP arrays as array-like collection objects similar to jQuery and Laravel Collections

4.3k459.4k15](/packages/aimeos-map)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
