array_group_by

A PHP function that groups an array by a key or set of keys shared between all array members.

  • Owner: jakezatecky/array_group_by
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

array_group_by

Packagist
Build Status
GitHub license

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

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

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

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;
});

Main metrics

Overview
Name With Ownerjakezatecky/array_group_by
Primary LanguagePHP
Program languagePHP (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2014-05-16 02:39:42
Pushed At2023-03-23 23:15:42
Last Commit At
Release Count7
Last Release Namev2.0.0 (Posted on )
First Release Name0.7.0 (Posted on 2014-05-16 22:51:57)
用户参与
Stargazers Count99
Watchers Count6
Fork Count16
Commits Count97
Has Issues Enabled
Issues Count6
Issue Open Count1
Pull Requests Count3
Pull Requests Open Count0
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private