【问题标题】:Why do i get Message: Illegal offset type and what does it mean为什么我会收到消息:非法偏移类型及其含义
【发布时间】:2013-09-29 19:41:35
【问题描述】:

在使用 CodeIgnator 时,当我尝试使用我的数据库时出现以下错误:

遇到了 PHP 错误

严重性:警告

消息:非法偏移类型

文件名:models/flight_model.php

行号:12

产生此错误的代码:

     $query = $this->db->query('SELECT Date, ID, Destination FROM flight');

       $flights= array();
         foreach ($query->result() as $row)
          {
                $flights[$row] = array('flight_id' => $row->ID, 'Date'=>$row->Date, 'Destination' => $row->Destination);
          } 

我不明白代码有什么问题:S

所有答案都得到认可

【问题讨论】:

    标签: php sql codeigniter codeigniter-2


    【解决方案1】:

    您收到此错误是因为$row 不是有效的数组键(我假设它是某种对象)。数组键只能是字符串或数字。

    您可以做的是使用行 ID 作为键:

    $flights[$row->ID] = ...
    

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 2011-08-07
      • 2019-08-11
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多