简体   繁体   English

如何在codeigniter中将查询结果作为一个数组返回

[英]how to return query result as one array in codeigniter

i have table (tag).. i want to select all tags in this table and return it as single array where tag_id = index and tag_name = value like this array 我有表(标签)..我想选择此表中的所有标签,并将其作为单个数组返回,其中tag_id =索引,并且tag_name =值,类似于此数组

array(
            3 => 'Hazel Grouse',
            4 => 'Common Quail',
            5 => 'Common Pheasant',
            6 => 'Northern Shoveler',
            7 => 'Greylag Goose',
            8 => 'Barnacle Goose',
            9 => 'Lesser Spotted Woodpecker',
            10 => 'Eurasian Pygmy-Owl',
            11 => 'Dunlin',
            13 => 'Black Scoter',
)

tags return like this multi D array 标签像这样的多D数组返回

Array
(
    [0] => Array
        (
            [tag_id] => 3
            [tag_name] => Hazel Grouse
        )

    [1] => Array
        (
            [tag_id] => 4
            [tag_name] => Common Quail
        )

this is query code 这是查询代码

 function get_alltags() {
        $this->db->select('tag_id');
        $this->db->select('tag_name');
        $result = $this->db->get('d_tag');
     return $result->result_array();
    }
    $alltags = $this->tag->get_alltags();
        echo "<pre>";
        print_r($alltags);
        echo "</pre>";
        exit;

you can do something like this 你可以做这样的事情

$result=array();
$alltags = $this->tag->get_alltags();
foreach($alltags as $k => $v){
$result[$v['tag_id']]=$v['tag_name'];}
echo "<pre>";
print_r($result);

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

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