Fusonic\Linq\Linq::toArray PHP Method

toArray() public method

Creates an Array from this Linq object with key/value selector(s).
public toArray ( callable $keySelector = null, callable $valueSelector = null ) : array
$keySelector callable a func that returns the array-key for each element.
$valueSelector callable a func that returns the array-value for each element.
return array An array with all values.
    public function toArray(callable $keySelector = null, callable $valueSelector = null)
    {
        if ($keySelector === null && $valueSelector === null) {
            return iterator_to_array($this, false);
        } elseif ($keySelector == null) {
            return iterator_to_array(new SelectIterator($this->getIterator(), $valueSelector), false);
        } else {
            $array = [];
            foreach ($this as $value) {
                $key = $keySelector($value);
                $array[$key] = $valueSelector == null ? $value : $valueSelector($value);
            }
            return $array;
        }
    }