Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jakezatecky/array_group_by
A PHP function that groups an array by a key or set of keys shared between all array members.
https://github.com/jakezatecky/array_group_by
composer php
Last synced: 5 days ago
JSON representation
A PHP function that groups an array by a key or set of keys shared between all array members.
- Host: GitHub
- URL: https://github.com/jakezatecky/array_group_by
- Owner: jakezatecky
- License: mit
- Created: 2014-05-16T02:39:42.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2023-03-23T23:15:42.000Z (almost 2 years ago)
- Last Synced: 2025-01-21T05:06:19.623Z (13 days ago)
- Topics: composer, php
- Language: PHP
- Homepage:
- Size: 53.7 KB
- Stars: 99
- Watchers: 6
- Forks: 17
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# array_group_by
[![Packagist](https://img.shields.io/packagist/v/jakezatecky/array_group_by.svg?style=flat-square)](https://packagist.org/packages/jakezatecky/array_group_by)
[![Build Status](https://img.shields.io/github/actions/workflow/status/jakezatecky/array_group_by/main.yml?branch=master&style=flat-square)](https://github.com/jakezatecky/array_group_by/actions/workflows/main.yml)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](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
To get the latest version of `array_group_by`, simply require the project using Composer:
``` shell
$ composer require jakezatecky/array_group_by
```Need support for PHP 5.6? Then run the following:
``` shell
$ 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:
``` php
$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:
``` text
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:
``` php
$grouped = array_group_by($records, function ($row) {
return $row->city;
});
```