• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Efficiently getting a one-dimensional array of results using ActiveRecord

#1
[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?

#2
[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.

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

#4
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.