4

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

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

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

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

$row->{0}$row[0]另一方面不是!

我仍然不确定要走哪条路线,如果有人能告诉我使用对象而不是数组的更多好处,我会很高兴!

4

1 回答 1

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.
  }
}
于 2013-05-24T03:07:41.423 回答