CodeIgniter Forums
Efficiently getting a one-dimensional array of results using ActiveRecord - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Efficiently getting a one-dimensional array of results using ActiveRecord (/showthread.php?tid=52990)



Efficiently getting a one-dimensional array of results using ActiveRecord - El Forum - 07-05-2012

[eluser]bientek[/eluser]
In my CI Models, I find myself frequently selecting a single column and then moving data from the object array into a one-diemsional array with a for-loop, like this:
Code:
$this->db->select('id');
$this->db->from('MyTable');
$rows = $this->db->get()->result();
$ids = array();
foreach ($row as $r)
$ids[] = $r->id;

Is there a better way?


Efficiently getting a one-dimensional array of results using ActiveRecord - El Forum - 07-05-2012

[eluser]Aken[/eluser]
Nope, that's pretty much the way to go. Only thing you might want to do is create a model method to do it, in case you find yourself repeating that code.


Efficiently getting a one-dimensional array of results using ActiveRecord - El Forum - 07-05-2012

[eluser]InsiteFX[/eluser]
Code:
$this->db->select('id');
$rows = $this->db->get('MyTable')->result();
$ids = array();
foreach ($row as $r)
$ids[] = $r->id;



Efficiently getting a one-dimensional array of results using ActiveRecord - El Forum - 07-05-2012

[eluser]bientek[/eluser]
Aken: Thanks for your reply.
InsiteFX: I see that you shortened the syntax, but I am more interested in the computational efficiency that could be gained from avoiding having to loop through the results to move them into a one-dimensional array.