不能使用 stdClass 类型的对象作为数组(php)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9546377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:05:20  来源:igfitidea点击:

Cannot use object of type stdClass as array(php)

phpjson

提问by akinboj

Possible Duplicate:
Fatal error: Cannot use object of type stdClass as array in

可能的重复:
致命错误:不能使用 stdClass 类型的对象作为数组

Please I keep getting this error: Fatal error: Cannot use object of type stdClass as array in C:\XAMMP\xampp\htdocs\Yemi\geograph\table.php on line 31

请我不断收到此错误:致命错误:无法在第 31 行的 C:\XAMMP\xampp\htdocs\Yemi\geograph\table.php 中使用 stdClass 类型的对象作为数组

This is the php I am trying to run:

这是我试图运行的 php:

php code

代码

回答by Shakti Singh

You are actually trying to access the object as array.

您实际上是在尝试将对象作为数组访问。

I can guess you are having problem when you are using json_decodewhich is returning an object and inside the foreachloop you are trying to access it like an associative array.

我猜你在使用json_decodewhich 返回一个对象时遇到了问题,并且在foreach循环中你试图像关联数组一样访问它。

Passing the second argument as trueto json_decodeforce it to return associative array.

传递第二个参数truejson_decode强制它返回关联数组。

Make the below change.

进行以下更改。

Change your this code line

更改您的此代码行

$items = json_decode($contents);

To

$items = json_decode($contents, true);