Lazer\Classes\Core_Database::asArray PHP Method

asArray() public method

Returning data as indexed or assoc array.
public asArray ( string $key = null, string $value = null ) : array
$key string Field that will be the key, NULL for Indexed
$value string Field that will be the value
return array
    public function asArray($key = null, $value = null)
    {
        if (!is_null($key)) {
            Helpers\Validate::table($this->name)->field($key);
        }
        if (!is_null($value)) {
            Helpers\Validate::table($this->name)->field($value);
        }
        $datas = array();
        if (!$this->resetKeys) {
            if (is_null($key) && is_null($value)) {
                return $this->data;
            } else {
                foreach ($this->data as $rowKey => $data) {
                    $datas[$rowKey] = array();
                    foreach ($data as $row) {
                        if (is_null($key)) {
                            $datas[$rowKey][] = $row->{$value};
                        } elseif (is_null($value)) {
                            $datas[$rowKey][$row->{$key}] = $row;
                        } else {
                            $datas[$rowKey][$row->{$key}] = $row->{$value};
                        }
                    }
                }
            }
        } else {
            if (is_null($key) && is_null($value)) {
                foreach ($this->data as $data) {
                    $datas[] = get_object_vars($data);
                }
            } else {
                foreach ($this->data as $data) {
                    if (is_null($key)) {
                        $datas[] = $data->{$value};
                    } elseif (is_null($value)) {
                        $datas[$data->{$key}] = $data;
                    } else {
                        $datas[$data->{$key}] = $data->{$value};
                    }
                }
            }
        }
        return $datas;
    }