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

aki*_*boj 11 php json

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

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

这是我想要运行的php:

PHP代码

Sha*_*ngh 41

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

我猜你正在使用json_decode哪个正在返回一个对象而foreach你正在尝试像一个关联数组一样访问它.

传递第二个参数true,json_decode强制它返回关联数组.

进行以下更改.

更改此代码行

$items = json_decode($contents);
Run Code Online (Sandbox Code Playgroud)

$items = json_decode($contents, true);
Run Code Online (Sandbox Code Playgroud)