All Projects → jakezatecky → Array_group_by

jakezatecky / Array_group_by

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

Labels

Projects that are alternatives of or similar to Array group by

Font Awesome Php
A PHP library for Font Awesome 4.7.
Stars: ✭ 47 (-38.96%)
Mutual labels:  composer
Pkgmirror
Packages Mirroring
Stars: ✭ 62 (-19.48%)
Mutual labels:  composer
Pusher Http Php
PHP library for interacting with the Pusher Channels HTTP API
Stars: ✭ 1,136 (+1375.32%)
Mutual labels:  composer
Php frameworks analysis
php框架源码分析
Stars: ✭ 57 (-25.97%)
Mutual labels:  composer
Mailchimp Api 3.0 Php
A feature rich object-oriented PHP library for interacting with MailChimp's API v3 💌🐵
Stars: ✭ 61 (-20.78%)
Mutual labels:  composer
Deb Dev Machine
Quickly install common Developer tools, IDE's & Services on Debian 9
Stars: ✭ 63 (-18.18%)
Mutual labels:  composer
Composer Patches
Simple patches plugin for Composer
Stars: ✭ 1,033 (+1241.56%)
Mutual labels:  composer
Html Compress Twig
Twig extension for compressing HTML and inline CSS/JS using WyriHaximus/HtmlCompress
Stars: ✭ 72 (-6.49%)
Mutual labels:  composer
Laravel Restify
The fastest way to make a powerful JSON:API compatible Rest API with Laravel.
Stars: ✭ 62 (-19.48%)
Mutual labels:  composer
Conductor
Conductor makes it easy to mange multiple composer packages within a single source repository
Stars: ✭ 64 (-16.88%)
Mutual labels:  composer
Spinupwp Composer Site
A WordPress site setup using Composer that is primed and ready to be hosted using SpinupWP.
Stars: ✭ 58 (-24.68%)
Mutual labels:  composer
Php
PHP 文章集锦,浮点数搞定度运算、签名验证、单点登录、安全防御、缓存技术、RPC、Composer ...
Stars: ✭ 61 (-20.78%)
Mutual labels:  composer
Scriptsdev
Scripts-dev directive for composer
Stars: ✭ 63 (-18.18%)
Mutual labels:  composer
Airflow Toolkit
Any Airflow project day 1, you can spin up a local desktop Kubernetes Airflow environment AND one in Google Cloud Composer with tested data pipelines(DAGs) 🖥 >> [ 🚀, 🚢 ]
Stars: ✭ 51 (-33.77%)
Mutual labels:  composer
Designpattern
设计模式
Stars: ✭ 66 (-14.29%)
Mutual labels:  composer
Larrock Core
Core components for LarrockCMS
Stars: ✭ 46 (-40.26%)
Mutual labels:  composer
Packagist Mirror
Alibaba Cloud Packagist Mirror
Stars: ✭ 63 (-18.18%)
Mutual labels:  composer
Composer Tools
⚠️ ⚠️ ⚠️ Hyperledger Composer has been deprecated ⚠️ ⚠️ ⚠️
Stars: ✭ 75 (-2.6%)
Mutual labels:  composer
Easy Short Url
ESU 短网址,可在 Laravel、Yii、ThinkPHP 等框架 Composer 包引入,也可以独立搭建短网址站点
Stars: ✭ 71 (-7.79%)
Mutual labels:  composer
Formula Parser
Parsing and evaluating mathematical formulas given as strings.
Stars: ✭ 62 (-19.48%)
Mutual labels:  composer

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;
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].