Codeigniter数据库结果 - 对象或数组?

时间:2013-05-23 12:30:38

标签: php database arrays codeigniter object

我一直在使用Codeigniter一段时间,我经常想知道哪些/是否使用Codeigniter的

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

就个人而言,我倾向于坚持使用Arrays,因为我发现操作数组和推送项目等更容易。唯一令我烦恼的是简单地或在视图中写下陈述,例如。

$row->title$row['title']

更清晰,更易于阅读

但是

另一方面

$row->{0}超过$row[0]不是!

如果有人能告诉我使用Object over Arrays的更多好处,我仍然处于不确定的状态,我很高兴!

1 个答案:

答案 0 :(得分:2)

我更喜欢对象,但使用可以在所有(或某些)列上执行某些检查或操作的情况下减少混乱的数组有一个好处:

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.
  }
}