Dibi\Result::fetchPairs PHP Method

fetchPairs() final public method

Fetches all records from table like $key => $value pairs.
final public fetchPairs ( $key = NULL, $value = NULL ) : array
return array
    public final function fetchPairs($key = NULL, $value = NULL)
    {
        $this->seek(0);
        $row = $this->fetch();
        if (!$row) {
            return [];
            // empty result set
        }
        $data = [];
        if ($value === NULL) {
            if ($key !== NULL) {
                throw new \InvalidArgumentException('Either none or both columns must be specified.');
            }
            // autodetect
            $tmp = array_keys($row->toArray());
            $key = $tmp[0];
            if (count($row) < 2) {
                // indexed-array
                do {
                    $data[] = $row[$key];
                } while ($row = $this->fetch());
                return $data;
            }
            $value = $tmp[1];
        } else {
            if (!property_exists($row, $value)) {
                throw new \InvalidArgumentException("Unknown value column '{$value}'.");
            }
            if ($key === NULL) {
                // indexed-array
                do {
                    $data[] = $row[$value];
                } while ($row = $this->fetch());
                return $data;
            }
            if (!property_exists($row, $key)) {
                throw new \InvalidArgumentException("Unknown key column '{$key}'.");
            }
        }
        do {
            $data[(string) $row[$key]] = $row[$value];
        } while ($row = $this->fetch());
        return $data;
    }