【问题标题】:Notice: Array to string conversion when inserting a stdClass into a stdClass注意:将 stdClass 插入 stdClass 时的数组到字符串转换
【发布时间】:2017-09-13 20:08:04
【问题描述】:

我正在尝试将数据库中的数据插入stdClass,但由于某种原因,我收到了Notice: Array to string conversion in data.php on line 47 的通知。

我正在创建 2 个stdClasses,每个车辆一个,一个包含所有车辆的将军。当我尝试将车辆的 stdClass 插入到通用时,我收到了通知。

这是我的代码(注意我在第 47 行写了):

$data = new stdClass();

while($row = mysqli_fetch_array($tresult)) {
    $vehicle = new stdClass();
    $vehicle->name = $row['name'];
    $vehicle->position = $row['position'];

    $data->$row['name'] = $vehicle; //Line 47
}

我在这里缺少什么? 谢谢

【问题讨论】:

  • 试试$data->{$row['name']} 或许?见:php.net/manual/en/language.variables.variable.php
  • 试试 var_dump($vehicle);要查看是数组还是字符串,请注意:)。
  • @ficuscr 你是个天才......谢谢你:D 写下它作为答案,我会尽快接受它
  • 一切都好。很高兴你把事情解决了。连接 OsDev。 "Same is good for array element output in a double quote string. {$foo[1]}"

标签: php stdclass


【解决方案1】:

尝试这样赋值,因为你必须插入变量

$data->{$row['name']} = $vehicle

【讨论】:

  • 谢谢你和 ficuscr,它已经解决了!我会在它允许的时候接受答案..
  • 只是给那些想了解更多关于它是如何工作的人的注释,这被称为Complex Syntax
猜你喜欢
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
  • 2020-03-20
相关资源
最近更新 更多