public static function groupBy(array $array, callable $callback = null): array
    {
        $callback = $callback ?: function ($value) {
            return $value;
        };

        return array_reduce(
            $array,
            function ($buckets, $value) use ($callback) {
                $key = $callback($value);

                if (! array_key_exists($key, $buckets)) {
                    $buckets[$key] = [];
                }

                $buckets[$key][] = $value;

                return $buckets;
            },
            []
        );
    }
public static function groupBy(array $array, callable $callback = null): array
    {
        $callback = $callback ?: function ($value) {
            return $value;
        };

        return array_reduce(
            $array,
            function ($buckets, $value) use ($callback) {
                $key = $callback($value);

                if (! array_key_exists($key, $buckets)) {
                    $buckets[$key] = [];
                }

                $buckets[$key][] = $value;

                return $buckets;
            },
            []
        );
    }
public static function groupBy ( array $ array , callable $ callback = null ) : array { $ callback = $ callback ? : function ( $ value ) { return $ value ; } ; return array_reduce ( $ array , function ( $ buckets , $ value ) use ( $ callback ) { $ key = $ callback ( $ value ) ; if ( ! array_key_exists ( $ key , $ buckets ) ) { $ buckets [ $ key ] = [ ] ; } $ buckets [ $ key ] [ ] = $ value ; return $ buckets ; } , [ ] ) ; }
Separates elements from an array into groups.
The function maps an element to the key that will be used for grouping.
If no function is passed, the element itself will be used as key.

@param array         $array
@param callable|null $callback

@return array
Separates elements from an array into groups . The function maps an element to the key that will be used for grouping . If no function is passed the element itself will be used as key .