无法将 stdClass 类型的对象用作数组
我有时会出现以下错误 致命错误:无法将 stdClass 类型的对象用作数组。 使用此功能:
function deliciousCount($domain_name)
{
$data = json_decode(
file_get_contents(
"http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
)
);
if ($data) {
return $data[0]->total_posts;
} else {
return 0;
}
}
$delic = deliciousCount($domain_name);
但此错误有时仅针对特定域发生 有什么帮助吗?
i have sometimes following error
Fatal error: Cannot use object of type stdClass as array in..
with this function:
function deliciousCount($domain_name)
{
$data = json_decode(
file_get_contents(
"http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
)
);
if ($data) {
return $data[0]->total_posts;
} else {
return 0;
}
}
$delic = deliciousCount($domain_name);
but this error happen sometimes only for specific domains
any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 手册,还有一个可选的第二个
boolean< /code> param,指定返回的对象是否应转换为关联数组(默认为false)。如果您想将其作为数组访问,则只需传递
true
作为第二个参数即可。According to the manual, there is an optional second
boolean
param which specifies whether the returned object should be converted to an associative array (default is false). If you want access it as an array then just passtrue
as the second param.在使用 $data 作为数组之前:
然后只需从数组中获取 Total_posts 值即可。
Before using $data as array:
And then simply get your total_posts value from array.
json_decode
返回 stdClass 的实例,您无法像访问数组一样访问它。json_decode
确实可以通过传递true
作为第二个参数来返回数组。json_decode
returns an instance of stdClass, which you can't access like you would access an array.json_decode
does have the possibility to return an array, by passingtrue
as a second parameter.或者
or