PHPackages                             jakezatecky/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. jakezatecky/array\_group\_by

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

jakezatecky/array\_group\_by
============================

A function that groups/splits an array by the values of a given key or keys.

v2.0.0(8y ago)99217.9k↓41.8%162MITPHPPHP &gt;=7.0CI failing

Since May 17Pushed 11mo ago5 watchersCompare

[ Source](https://github.com/jakezatecky/array_group_by)[ Packagist](https://packagist.org/packages/jakezatecky/array_group_by)[ Docs](https://github.com/jakezatecky/array_group_by)[ RSS](/packages/jakezatecky-array-group-by/feed)WikiDiscussions main Synced 2w ago

READMEChangelogDependencies (1)Versions (11)Used By (2)

array\_group\_by
================

[](#array_group_by)

[![Packagist](https://camo.githubusercontent.com/283b9ddfc40c0ea7cf058db8c7536ccd5091fb23e68cf5345a9644de830b66de/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a616b657a617465636b792f61727261795f67726f75705f62792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jakezatecky/array_group_by)[![Build Status](https://camo.githubusercontent.com/02fe9d8fef0ded5c5c62dbe9c66f54954d74ec22a2484e1be60a3cc41c01a165/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a616b657a617465636b792f61727261795f67726f75705f62792f6d61696e2e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/jakezatecky/array_group_by/actions/workflows/main.yml)[![GitHub license](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](https://raw.githubusercontent.com/jakezatecky/array_group_by/master/LICENSE.txt)

> A PHP function to group an array by a key or set of keys shared between all array members.

Installation
------------

[](#installation)

To get the latest version of `array_group_by`, simply require the project using Composer:

```
$ composer require jakezatecky/array_group_by
```

Need support for PHP 5.6? Then run the following:

```
$ composer require jakezatecky/array_group_by:^1.1.0
```

If you do not want to use Composer, you can just `require` the `src/array_group_by.php` file.

Usage
-----

[](#usage)

To use `array_group_by`, simply pass an array with any number of keys to group by:

```
$records = [
    [
        'state'  => 'IN',
        'city'   => 'Indianapolis',
        'object' => 'School bus',
    ],
    [
        'state'  => 'IN',
        'city'   => 'Indianapolis',
        'object' => 'Manhole',
    ],
    [
        'state'  => 'IN',
        'city'   => 'Plainfield',
        'object' => 'Basketball',
    ],
    [
        'state'  => 'CA',
        'city'   => 'San Diego',
        'object' => 'Light bulb',
    ],
    [
        'state'  => 'CA',
        'city'   => 'Mountain View',
        'object' => 'Space pen',
    ],
];

$grouped = array_group_by($records, 'state', 'city');
```

Example output:

```
Array
(
    [IN] => Array
        (
            [Indianapolis] => Array
                (
                    [0] => Array
                        (
                            [state] => IN
                            [city] => Indianapolis
                            [object] => School bus
                        )

                    [1] => Array
                        (
                            [state] => IN
                            [city] => Indianapolis
                            [object] => Manhole
                        )

                )

            [Plainfield] => Array
                (
                    [0] => Array
                        (
                            [state] => IN
                            [city] => Plainfield
                            [object] => Basketball
                        )

                )

        )

    [CA] => Array
        (
            [San Diego] => Array
                (
                    [0] => Array
                        (
                            [state] => CA
                            [city] => San Diego
                            [object] => Light bulb
                        )

                )

            [Mountain View] => Array
                (
                    [0] => Array
                        (
                            [state] => CA
                            [city] => Mountain View
                            [object] => Space pen
                        )

                )

        )
)

```

### Using a Callback

[](#using-a-callback)

If more complex grouping behavior is desired, you can also pass in a callback function to determine the group key:

```
$grouped = array_group_by($records, function ($row) {
    return $row->city;
});
```

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity50

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 97% 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 ~177 days

Recently: every ~203 days

Total

8

Last Release

3236d ago

Major Versions

0.7.3 → 1.0.02015-09-04

v1.1.0 → v2.0.02017-10-07

PHP version history (3 changes)0.7.3PHP &gt;=5.3

1.0.0PHP &gt;=5.5

v2.0.0PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/50781e90d82b52759199f2363673607ccd9afdf1f977c9207ddc777b5981dc55?d=identicon)[jzatecky](/maintainers/jzatecky)

---

Top Contributors

[![jakezatecky](https://avatars.githubusercontent.com/u/3210703?v=4)](https://github.com/jakezatecky "jakezatecky (96 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (2 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")

---

Tags

composerphparraysplitgrouparray\_group\_byarray\_key\_split

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[doctrine/collections

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

6.0k437.1M1.6k](/packages/doctrine-collections)[symfony/property-access

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

2.8k325.8M3.5k](/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.1k443.7M1.8k](/packages/nette-utils)[league/config

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

567347.0M41](/packages/league-config)[cuyz/valinor

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

1.5k14.6M202](/packages/cuyz-valinor)[aimeos/map

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

4.3k475.2k19](/packages/aimeos-map)

PHPackages © 2026

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