public function group($grouper, bool $saveKeys = false): self
    {
        // init
        $result = [];

        // Iterate over values, group by property/results from closure.
        foreach ($this->getGenerator() as $key => $value) {
            if (\is_callable($grouper)) {
                $groupKey = $grouper($value, $key);
            } else {
                $groupKey = $this->get($grouper, null, $this->getArray());
            }

            $newValue = $this->get($groupKey, null, $result);

            if ($groupKey instanceof self) {
                $groupKey = $groupKey->getArray();
            }

            if ($newValue instanceof self) {
                $newValue = $newValue->getArray();
            }

            // Add to results.
            if ($groupKey !== null) {
                if ($saveKeys) {
                    $result[$groupKey] = $newValue;
                    $result[$groupKey][$key] = $value;
                } else {
                    $result[$groupKey] = $newValue;
                    $result[$groupKey][] = $value;
                }
            }
        }

        return static::create(
            $result,
            $this->iteratorClass,
            false
        );
    }
public function group($grouper, bool $saveKeys = false): self
    {
        // init
        $result = [];

        // Iterate over values, group by property/results from closure.
        foreach ($this->getGenerator() as $key => $value) {
            if (\is_callable($grouper)) {
                $groupKey = $grouper($value, $key);
            } else {
                $groupKey = $this->get($grouper, null, $this->getArray());
            }

            $newValue = $this->get($groupKey, null, $result);

            if ($groupKey instanceof self) {
                $groupKey = $groupKey->getArray();
            }

            if ($newValue instanceof self) {
                $newValue = $newValue->getArray();
            }

            // Add to results.
            if ($groupKey !== null) {
                if ($saveKeys) {
                    $result[$groupKey] = $newValue;
                    $result[$groupKey][$key] = $value;
                } else {
                    $result[$groupKey] = $newValue;
                    $result[$groupKey][] = $value;
                }
            }
        }

        return static::create(
            $result,
            $this->iteratorClass,
            false
        );
    }
public function group ( $ grouper , bool $ saveKeys = false ) : self { // init $ result = [ ] ; // Iterate over values, group by property/results from closure. foreach ( $ this -> getGenerator ( ) as $ key => $ value ) { if ( \ is_callable ( $ grouper ) ) { $ groupKey = $ grouper ( $ value , $ key ) ; } else { $ groupKey = $ this -> get ( $ grouper , null , $ this -> getArray ( ) ) ; } $ newValue = $ this -> get ( $ groupKey , null , $ result ) ; if ( $ groupKey instanceof self ) { $ groupKey = $ groupKey -> getArray ( ) ; } if ( $ newValue instanceof self ) { $ newValue = $ newValue -> getArray ( ) ; } // Add to results. if ( $ groupKey !== null ) { if ( $ saveKeys ) { $ result [ $ groupKey ] = $ newValue ; $ result [ $ groupKey ] [ $ key ] = $ value ; } else { $ result [ $ groupKey ] = $ newValue ; $ result [ $ groupKey ] [ ] = $ value ; } } } return static :: create ( $ result , $ this -> iteratorClass , false ) ; }
Group values from a array according to the results of a closure.

@param callable|string $grouper  <p>A callable function name.</p>
@param bool            $saveKeys

@return static
<p>(Immutable)</p>
Group values from a array according to the results of a closure .