YaLinqo\Enumerable::groupBy PHP Method

groupBy() public method

Syntax: groupBy ()

Groups the elements of a sequence by its keys.

Syntax: groupBy (keySelector {(v, k) ==> key})

Groups the elements of a sequence according to a specified key selector function.

Syntax: groupBy (keySelector {(v, k) ==> key}, valueSelector {(v, k) ==> value})

Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.

Syntax: groupBy (keySelector {(v, k) ==> key}, valueSelector {(v, k) ==> value}, resultSelectorValue {(e, k) ==> value} [, resultSelectorKey {(e, k) ==> key}])

Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key.

For all overloads except the last: the groupBy method returns a sequence of sequences, one inner sequence for each distinct key that was encountered. The outer sequence is yielded in an order based on the order of the elements in source that produced the first key of each inner sequence. Elements in a inner sequence are yielded in the order they appear in source.

public groupBy ( callable | null $keySelector = null, callable | null $valueSelector = null, callable | null $resultSelectorValue = null, callable | null $resultSelectorKey = null ) : Enumerable
$keySelector callable | null {(v, k) ==> key} A function to extract the key for each element. Default: key.
$valueSelector callable | null {(v, k) ==> value} A function to map each source element to a value in the inner sequence.
$resultSelectorValue callable | null {(e, k) ==> value} A function to create a result value from each group.
$resultSelectorKey callable | null {(e, k) ==> key} A function to create a result key from each group.
return Enumerable A sequence of sequences indexed by a key.
    public function groupBy($keySelector = null, $valueSelector = null, $resultSelectorValue = null, $resultSelectorKey = null)
    {
        $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$key);
        $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value);
        $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'e,k', Functions::$value);
        $resultSelectorKey = Utils::createLambda($resultSelectorKey, 'e,k', Functions::$key);
        return self::from($this->toLookup($keySelector, $valueSelector))->select($resultSelectorValue, $resultSelectorKey);
    }