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

时间:2014-07-05 16:46:08

标签: php mysql codeigniter

我有桌子(标签).. 我想选择此表中的所有标记并将其作为单个数组返回,其中tag_id = index 和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',
)

标签像这个多D数组一样返回

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

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

这是查询代码

 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;

1 个答案:

答案 0 :(得分:2)

你可以做这样的事情

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