【问题标题】:Strange stdClass as array error when the variable seems to be an array, not an object当变量似乎是数组而不是对象时,奇怪的 stdClass 作为数组错误
【发布时间】:2015-11-22 11:57:42
【问题描述】:

我注意到的一些问题可以解决问题: 如果我返回 $this->_data 而不是 $this->_data[0] - 然后使用 $object->data()[0],它会按预期工作......但是,我想在函数中返回 [0]。

在您开始说这是重复之前 - 这个问题与大多数其他有关此错误的问题不同。

在我的班级中尝试访问 $this->_data[0] 时出现以下错误:

Cannot use object of type stdClass as array

这是触发错误的函数/行:

  public function data(){
      return $this->_data[0]; //<- line 75
  }

据我了解,当我尝试像使用数组一样使用对象时会发生此错误。但是,当我var_dump($this-&gt;_data) 时,我得到以下结果:

array(1) {
    [0]=> object(stdClass)#34 (10) { 
        ["uid"]=> string(1) "0"
        ["nid"]=> string(3) "374"
        ["id"]=> string(8) "YicnaxYw"
        ["txt_content"]=> NULL
        ["path"]=> string(32) "/uploads/images/png/YicnaxYw.png"
        ["image"]=> string(1) "1"
        ["timestamp"]=> string(10) "1448192959"
        ["file_ext"]=> string(3) "png"
        ["file_type"]=> string(9) "image/png"
        ["originalFilename"]=> string(23) "2015-11-22_12-49-17.png"
    }
}

很明显,这个变量是array(1) 类型,带有[0] 元素..

谁能告诉我我做错了什么?

提前致谢。

@Xeli $this-&gt;_data[0]-&gt;nid 导致同样的错误


致命错误:在 75 行的 /home3/ramzes/includes/classes/UploadItem.php 中无法使用 stdClass 类型的对象作为数组

UploadItem.php:75

  public function data(){
      return $this->_data[0]->nid;
  }

$this-&gt;_data[0] 是一个带有查询结果的 fetchAll(PDO::FETCH_OBJ) 对象

【问题讨论】:

  • 能否请您发布触发错误的完整代码行?
  • 你确定这样的东西不起作用吗? echo $this->_data[0]->nid;
  • 检查编辑!我不知道为什么会发生这个错误,因为自昨天和昨天以来我并没有真正改变太多代码,这完全相同的事情就像它应该的那样工作
  • 如果我将 [0] 移动到函数的结果中,它的工作方式就像它应该的那样。检查帖子顶部的编辑。知道为什么会这样吗?
  • 您是否多次调用查询?也许对 data() 的第一次调用是一个空集,因此 $this->_data 是空的,而在第二次调用中 $this->_data[0] 已设置,因此不会发生错误

标签: php arrays stdclass


【解决方案1】:

编辑:

我已经在http://ideone.com/66I3wO 上为此设置了一个测试 - 我将您的对象存储在一个数组中,我可以访问 $this->_data[0]->nid 就好了。

<?php

class Test
{
    public $_data = [];

    public function __construct() {
        $this->_data[0] = (object) [
            'uid' => '0',
            'nid' => '374',
            'id' => 'YicnaxYw',
            'txt_content' => null,
            'path' => '/uploads/images/png/YicnaxYw.png',
            'image' => '1',
            'timestamp' => '1448192959',
            'file_ext' => 'png',
            'file_type' => 'image/png',
            'originalFilename' => '2015-11-22_12-49-17.png'
        ];
    }

    public function data(){
        return $this->_data[0];
    }
}

$test = new Test();

echo $test->_data[0]->nid;

var_dump( $test->data() );

【讨论】:

  • 谢谢 - 我已将问题范围缩小到 PDO 将多个对象作为对象而不是作为对象数组返回的方式
【解决方案2】:

我认为 PDO 返回一个通过以下方式实现的对象:http://php.net/manual/en/class.iteratoraggregate.php

当你 var_dump 时,它看起来像一个数组,但它不是。

您可以将数据函数更改为:

function data() {
    foreach($this->_data as $data) {
        return $data;
    }
}

【讨论】:

    【解决方案3】:

    如果你显式转换为数组,那么你可以像你想要的那样访问索引:

    $return = (array) $this->_data;
    return $return[0];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-05
      • 2018-10-04
      • 2017-09-28
      • 2019-12-13
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      相关资源
      最近更新 更多