I'm using a query in Codeigniter to return the ids of all the rows that belong to a user.
$this->db->select('id');
$this->db->where('user_id', 99);
$query = $this->db->get('my_table');
return $query->result_array();
This returns
Array ( [0] => Array ( [id] => 8 ) [1] => Array ( [id] => 7 ) [2] => Array ( [id] => 6 ) )
Is it possible to return a flat array like
Array ( [0] => 8 [1] => 6 [2] => 7 )
?
$b = array();
foreach($query->result_array() as $a) {
$b[] = $a['id'];
}
return $b;
If you're to lazy to add few lines each time you select one column, you would need to tweak Codeigniter. Like adding some option or returning flattened array when single column is selected. I would sugest adding an option
Following can be another way of dong it. I don't know what is the advantage of it over the accepted answer.
Let's say you are storing output of function in $result
. Then you can do as following:
array_unshift($result, null);
$transposed = call_user_func_array("array_map", $result);
print_r($transposed);
It will print
Array( [0] => Array ( [0] => 8 [1] => 6 [2] => 7 ))
And you can access what you want by $transposed[0]
Array ( [0] => 8 [1] => 6 [2] => 7 )
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.