简体   繁体   English

Codeigniter选择结果数组格式

[英]codeigniter select result array format

I have this code: 我有以下代码:

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

It's return value in this format: 它是以下格式的返回值:

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

And I want it to return simple array like Array(2,3,5) 我希望它返回像Array(2,3,5)这样的简单数组

(Without arrays inside arrays and without the "cat_id" index) (没有数组内部的数组,也没有“ cat_id”索引)

What do I need to change? 我需要更改什么?

replace your return $query; 替换您的return $query; with this return array_map( function( $data ) { return $data['cat_id']; }, $query ); 使用此return array_map( function( $data ) { return $data['cat_id']; }, $query );

result_array() returns a json_decoded array. result_array()返回一个json_decoded数组。 Use json_encode($query) 使用json_encode($query)

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

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