2017 © Pedro Peláez
 

library array_group_by

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

image

jakezatecky/array_group_by

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

  • Saturday, October 7, 2017
  • by jzatecky
  • Repository
  • 3 Watchers
  • 51 Stars
  • 7,195 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 10 Forks
  • 1 Open issues
  • 10 Versions
  • 19 % Grown

The README.md

array_group_by

Packagist Build Status GitHub license, (*1)

A PHP function to group an array by a key or set of keys shared between all array members., (*2)

Installation

To get the latest version of array_group_by, simply require the project using Composer:, (*3)

``` shell $ composer require jakezatecky/array_group_by, (*4)


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., (*5)

Usage

To use array_group_by, simply pass an array with any number of keys to group by:, (*6)

``` 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', ], ];, (*7)

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


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:, (*9)

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

The Versions

07/10 2017

v1.x-dev

1.9999999.9999999.9999999-dev https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split

07/10 2017

dev-master

9999999-dev https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split

07/10 2017

v2.0.0

2.0.0.0 https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split

07/10 2017

v1.1.0

1.1.0.0 https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split

04/09 2015

1.0.0

1.0.0.0 https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split

01/09 2015

dev-dev-1.0

dev-dev-1.0 https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split

17/07 2015

0.7.3

0.7.3.0 https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split

23/10 2014

0.7.2

0.7.2.0 https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split

19/05 2014

0.7.1

0.7.1.0 https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split

17/05 2014

0.7.0

0.7.0.0 https://github.com/jakezatecky/array_group_by

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

  Sources   Download

MIT

The Development Requires

by Jake Zatecky

array group split array_group_by array_key_split