简体   繁体   English

将结果行的列作为数组返回,可通过代码初始化/活动记录中的索引引用

[英]return the columns of a result row as an array that can be referenced by index in codeigniter / active record

I am getting a specific row in a database with codeigniter / php /active record. 我正在使用codeigniter / php / active记录在数据库中获取特定行。 I a would like to return the result as an array of the row's columns that can be referenced like this: 我想以行列的数组形式返回结果,可以像这样引用:

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

This is my current query: 这是我当前的查询:

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);

But currently, to echo these, I (think) I need to give the name of the column, like: 但是目前,为了回应这些,我(认为)我需要给出列的名称,例如:

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

Is there a way to do this so I can say: 有没有办法做到这一点,所以我可以说:

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

You can do this by create a new array and assigns all values of old array to new array. 您可以通过创建一个新数组并将旧数组的所有值分配给新数组来实现。

You can use the array_values() function to do this. 您可以使用array_values()函数执行此操作。 Here is the example. 这是例子。 so you can get more idea. 这样您可以获得更多的想法。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM