不能使用 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
Cannot use object of type stdClass as array(php)
提问by akinboj
Possible Duplicate:
Fatal error: Cannot use object of type stdClass as array in
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:
回答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_decode
which is returning an object and inside the foreach
loop you are trying to access it like an associative array.
我猜你在使用json_decode
which 返回一个对象时遇到了问题,并且在foreach
循环中你试图像关联数组一样访问它。
Passing the second argument as true
to json_decode
force it to return associative array.
传递第二个参数true
以json_decode
强制它返回关联数组。
Make the below change.
进行以下更改。
Change your this code line
更改您的此代码行
$items = json_decode($contents);
To
到
$items = json_decode($contents, true);