Skip to content

[12.x] Added Arr::groupBy() helper function for array grouping #55472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

vishal2931
Copy link
Contributor

Overview

This PR introduces a new Arr::groupBy() helper method to the Illuminate\Support\Arr class. It provides a convenient way to group associative array by a specified key or a callback, with optional support for preserving the original array keys.

Example

use Illuminate\Support\Arr;

$products = [
    [
        'id' => 1,
        'type' => 'Electronics',
        'name' => 'Laptop',
    ],
    [
        'id' => 2,
        'type' => 'Clothing',
        'name' => 'T-shirt',
    ],
    [
        'id' => 3,
        'type' => 'Electronics',
        'name' => 'Smartphone',
    ],
];

// Group by type
$grouped = Arr::groupBy($products, 'type');

/*
Output: 
[
    'Electronics' => [
        [
            'id' => 1,
            'type' => 'Electronics',
            'name' => 'Laptop',
        ],
        [
            'id' => 3,
            'type' => 'Electronics',
            'name' => 'Smartphone',
        ],
    ],
    'Clothing' => [
        [
            'id' => 2,
            'type' => 'Clothing',
            'name' => 'T-shirt',
        ],
    ],
]
*/

// With preserved keys
$grouped = Arr::groupBy($products, 'type', true);

/*
Output: 
[
    'Electronics' => [
        0 => [
            'id' => 1,
            'type' => 'Electronics',
            'name' => 'Laptop',
        ],
        2 => [
            'id' => 3,
            'type' => 'Electronics',
            'name' => 'Smartphone',
        ],
    ],
    'Clothing' => [
        1 => [
            'id' => 2,
            'type' => 'Clothing',
            'name' => 'T-shirt',
        ],
    ],
]
*/

// With callback
$grouped = Arr::groupBy($products, function ($item) {
    return strtolower($item['type']);
});

/*
Output:
[
    'electronics' => [
        [
            'id' => 1,
            'type' => 'Electronics',
            'name' => 'Laptop',
        ],
        [
            'id' => 3,
            'type' => 'Electronics',
            'name' => 'Smartphone',
        ],
    ],
    'clothing' => [
        [
            'id' => 2,
            'type' => 'Clothing',
            'name' => 'T-shirt',
        ],
    ],
]
*/

@shaedrich
Copy link
Contributor

shaedrich commented Apr 18, 2025

By doing this, we would end up having groupBy() on Arr while having mapToGroups() on Collection—not sure if we want to different approaches 🤔

@rodrigopedra
Copy link
Contributor

@shaedrich, collections also have a groupBy method, with a similar implementation.

https://laravel.com/docs/12.x/collections#method-groupby

My guess is that @vishal2931 took inspiration from that implementation.

@shaedrich
Copy link
Contributor

Ah, thanks for the hint—nevermind 🤦🏻

$result = [];

foreach ($array as $key => $item) {
$groupKey = is_callable($groupBy) ? $groupBy($item, $key) : static::get($item, $groupBy);
Copy link
Contributor

@rodrigopedra rodrigopedra Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would ensure $groupBy is a callback out the loop, so we don't need to check if it is a callback on every item.

Something like this:

$groupBy = is_callable($groupBy) ? $groupBy : fn ($item) => static::get($item, $groupBy);

foreach ($array as $key => $item) {
    $groupKey = $groupBy($item, $key);

    // ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, but that approach looks more readable than this one. 🤔

@vishal2931
Copy link
Contributor Author

@shaedrich, collections also have a groupBy method, with a similar implementation.

https://laravel.com/docs/12.x/collections#method-groupby

My guess is that @vishal2931 took inspiration from that implementation.

@rodrigopedra Correct. It will work same as Collection groupBy.

@vishal2931
Copy link
Contributor Author

By doing this, we would end up having groupBy() on Arr while having mapToGroups() on Collection—not sure if we want to different approaches 🤔

Yes you're right. I was thinking that instead of transforming it into a collection and then back to an array, we can do it directly on the array. What do you think? 🤔

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants