src/Collection/ArrayCollection.php
public function groupBy(callable $callback, bool $preserveKeys = false): ArrayCollection
    {
        $items = [];

        foreach ($this->items as $key => $value) {
            $group = $callback($value, $key);

            if (!array_key_exists($group, $items)) {
                $items[$group] = new static();
            }

            $items[$group]->set($preserveKeys ? $key : null, $value);
        }

        return new static($items);
    }
public function groupBy(callable $callback, bool $preserveKeys = false): ArrayCollection
    {
        $items = [];

        foreach ($this->items as $key => $value) {
            $group = $callback($value, $key);

            if (!array_key_exists($group, $items)) {
                $items[$group] = new static();
            }

            $items[$group]->set($preserveKeys ? $key : null, $value);
        }

        return new static($items);
    }
public function groupBy ( callable $ callback , bool $ preserveKeys = false ) : ArrayCollection { $ items = [ ] ; foreach ( $ this -> items as $ key => $ value ) { $ group = $ callback ( $ value , $ key ) ; if ( ! array_key_exists ( $ group , $ items ) ) { $ items [ $ group ] = new static ( ) ; } $ items [ $ group ] -> set ( $ preserveKeys ? $ key : null , $ value ) ; } return new static ( $ items ) ; }
Creates a collection grouped by keys from a callback

Callback signature:

<code>
function ($value, $key): mixed {}
</code>

@param callable $callback     The callback function
@param bool     $preserveKeys Whether to preserve keys

@return ArrayCollection
Creates a collection grouped by keys from a callback