编码器数据库结果-对象或数组


Codeigniter Database results - Objects or Arrays?

我已经使用Codeigniter很长一段时间了,我经常想知道哪个/是否使用Codeigniter的

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

就我个人而言,我倾向于坚持使用数组,因为我发现它更容易操作数组和推入项等。唯一让我有点恼火的是在视图中写语句太简单了,例如

$row->title$row['title']更清晰,更容易阅读

,

$row->{0}优于$row[0],而不是!

我仍然不知道该走哪条路,如果有人能告诉我使用对象比数组有更多的好处,我会很高兴的!

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

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