Welcome Guest, Not a member yet? Register   Sign In
Removing the keys from result_array()
#1

[eluser]matula[/eluser]
This is probably a simple task, but I want to generate a single-level array from a resultset. This code:
Code:
$query = $this->db->select('id')->from('table')->get();
return $query->result_array()
... produces an array with "id" as the key. But I'd like to return a flat array like: array(3,5,8,12,etc)

Is there a way to do that in CI or PHP, or will I need to just loop through the array and create it myself like:
Code:
$myarray = array();
foreach($query->result_array() as $result){
  $myarray[] = $result['id'];
}

Thanks!
#2

[eluser]jwburnside[/eluser]
I believe PHP has a function to cover you there, called array_values():

http://php.net/manual/en/function.array-values.php
#3

[eluser]jwburnside[/eluser]
Actually, I'm sorry, I misunderstood the question. That will just create numerical keys. As far as I know you will have to loop through to array to get what you want.
#4

[eluser]Coode[/eluser]
This is something I didn't try out myself, but give it a shot!

Code:
<?php
   $r = blah blah->result_array();
   $ids = array_map('intval', array_map('end', $r));
?>

The variable would be array(1, 2, 3, 4). Smile




Theme © iAndrew 2016 - Forum software by © MyBB