Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been using Codeigniter for a good while now and i've often wondered which/whether to use Codeigniter's

$query->result() or $query->result_array()

Personally, I've tended to stick with Arrays as I find it easier to manipulate the array and push items and so forth. The only thing that annoys me slightly is the simply or writing the statements in the view e.g.

$row->title is a lot cleaner and easier to read than $row['title']

but

$row->{0} over $row[0] on the other hand isn't!

I'm still in limbo as to what route to go down, if anyone can tell me more benefits of using Objects over Arrays I'd be delighted!

share|improve this question
 
If you like OOPS than object have the edge –  Pramod May 23 at 12:43
 
It's entirely up to you - if you prefer working with arrays, stick with them, if you prefer OOP, objects will look nicer. –  Shomz May 23 at 12:54
1  
Just pick one. Whichever you want to use is fine. Also, why would you ever use $row->{0}? $query->result() returns an array of objects. –  Rocket Hazmat May 23 at 13:34
 
If you ever has a database column named a number e.g. 0 | 1 | 2 (for whatever reason - I've used some strange API's which have done!) then you need to reference using the bracket example above –  Timmytjc May 24 at 11:21

1 Answer

I prefer objects, but there is a benefit to using arrays that can reduce clutter in situations that call for performing some check or operation on all (or some) columns:

foreach ($row as $column => $value) { 
  if ($value == null) {
    // oh no!  null!
  }
}

// check these columns and make sure they're non-negative
$non_negative = array('amount_paid', 'income');

foreach ($non_negative as $column) {
  if ($row[$column] < 0) {
    // less than zero?  what a bum.
  }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.