YaLinqo\Enumerable::toDictionary PHP Method

toDictionary() public method

Syntax: toDictionary ([keySelector {(v, k) ==> key} [, valueSelector {(v, k) ==> value}]])

The toDictionary method returns an array, a one-to-one dictionary that maps keys to values. If the source sequence contains multiple values with the same key, the result array will only contain the latter value.

public toDictionary ( callable | null $keySelector = null, callable | null $valueSelector = null ) : array
$keySelector callable | null {(v, k) ==> key} A function to extract a key from each element. Default: key.
$valueSelector callable | null {(v, k) ==> value} A transform function to produce a result value from each element. Default: value.
return array An array that contains keys and values selected from the input sequence.
    public function toDictionary($keySelector = null, $valueSelector = null)
    {
        $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$key);
        $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value);
        $dic = [];
        foreach ($this as $k => $v) {
            $dic[$keySelector($v, $k)] = $valueSelector($v, $k);
        }
        return $dic;
    }