简体   繁体   中英

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)

(Without arrays inside arrays and without the "cat_id" index)

What do I need to change?

replace your return $query; with this return array_map( function( $data ) { return $data['cat_id']; }, $query );

result_array() returns a json_decoded array. Use json_encode($query)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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