vendor/laravel/framework/src/Illuminate/Support/Collection.php
/**
     * Group an associative array by a field or using a callback.
     *
     * @param  callable|string  $groupBy
     * @param  bool  $preserveKeys
     * @return static
     */
Group an associative array by a field or using a callback.
@param  callable|string  $groupBy
@param  bool  $preserveKeys
@return static
Group an associative array by a field or using a callback . @param callable|string $groupBy @param bool $preserveKeys @return static
public function groupBy($groupBy, $preserveKeys = false)
    {
        $groupBy = $this->valueRetriever($groupBy);
        $results = [];
        foreach ($this->items as $key => $value) {
            $groupKeys = $groupBy($value, $key);
            if (! is_array($groupKeys)) {
                $groupKeys = [$groupKeys];
            }
            foreach ($groupKeys as $groupKey) {
                $groupKey = is_bool($groupKey) ? (int) $groupKey : $groupKey;
                if (! array_key_exists($groupKey, $results)) {
                    $results[$groupKey] = new static;
                }
                $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value);
            }
        }
        return new static($results);
    }
public function groupBy ( $ groupBy , $ preserveKeys = false ) { $ groupBy = $ this -> valueRetriever ( $ groupBy ) ; $ results = [ ] ; foreach ( $ this -> items as $ key => $ value ) { $ groupKeys = $ groupBy ( $ value , $ key ) ; if ( ! is_array ( $ groupKeys ) ) { $ groupKeys = [ $ groupKeys ] ; } foreach ( $ groupKeys as $ groupKey ) { $ groupKey = is_bool ( $ groupKey ) ? ( int ) $ groupKey : $ groupKey ; if ( ! array_key_exists ( $ groupKey , $ results ) ) { $ results [ $ groupKey ] = new static ; } $ results [ $ groupKey ] -> offsetSet ( $ preserveKeys ? $ key : null , $ value ) ; } } return new static ( $ results ) ; }
Group an associative array by a field or using a callback.
Group an associative array by a field or using a callback .