首页 文章

将结果行的列作为可在codeigniter / active record中的索引引用的数组返回

提问于
浏览
0

我在codeigniter / php / active记录的数据库中得到一个特定的行 . 我想将结果作为行的列数组返回,可以像这样引用:

result[0] = column1
result[1] = column2
...

这是我目前的查询:

public function get_instance($instanceID = NULL){
    $this->db->select('organism, feature, goal');
    $this->db->from('extra_instances');
    $this->db->where('id', $instanceID);
    $query = $this->db->get();
    return $query->row_array();
}

$data['instance'] = $this->instances_model->get_instance($instanceID);

但是目前,为了回应这些,我(想)我需要给出列的名称,如:

<?php echo($instance['goal']) ; ?>

有没有办法做到这一点,所以我可以说:

<?php echo($instance[2]) ; ?>

1 回答

  • 0

    您可以通过创建一个新数组并将旧数组的所有值分配给新数组来完成此操作 .

    您可以使用array_values()函数执行此操作 . 这是一个例子 . 所以你可以得到更多的想法 .

    public function get_instance($instanceID = NULL){
        $this->db->select('organism, feature, goal');
        $this->db->from('extra_instances');
        $this->db->where('id', $instanceID);
        $query = $this->db->get();
        $results = $query->row_array();
        return array_values($results);
    }
    

相关问题