【问题标题】:codeigniter select result array formatcodeigniter 选择结果数组格式
【发布时间】:2015-12-16 16:47:22
【问题描述】:

我有这个代码:

 $query = $this->db->select( 'cat_id' )
            ->from( 'products_cat' )
            ->where('product_id',$id)
            ->get()
            **->result_array();**
return $query;

它是这种格式的返回值:

Array ( [0] => Array ( [cat_id] => 2 ) [1] => Array ( [cat_id] => 3 ) [2] => Array ( [cat_id] => 5 ) )

我希望它返回像Array(2,3,5)这样的简单数组

(数组内没有数组,也没有“cat_id”索引)

我需要改变什么?

【问题讨论】:

    标签: php mysql codeigniter select activerecord


    【解决方案1】:

    result_array() 返回一个 json_decoded 数组。使用json_encode($query)

    【讨论】:

      【解决方案2】:

      替换你的return $query; 有了这个 return array_map( function( $data ) { return $data['cat_id']; }, $query );

      【讨论】:

      • 它工作。谢谢。但这真的是最好的方法吗?
      猜你喜欢
      • 2017-01-11
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多