php再调用json_decode从字符串对象生成json对象时出现Cannot use object of type stdClass as array解决方法

admin5年前php994

php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误:

Cannot use object of type stdClass as array

产生原因:

一.有其他的不正常输出,好好检查一下删除其余的输出就可以了


二.解决方法(2种):

$res = json_decode($res);
$res['key']; //把 json_decode() 后的对象当作数组使用。


1、使用 json_decode($d, true)。就是使json_decode 的第二个变量设置为 true。

2、json_decode($res) 返回的是一个对象, 不可以使用 $res['key'] 进行访问, 换成 $res->key 就可以了。


相关文章

php调用chatgpd接口输出

<?php $url = 'https://api.openai.com/v1/completions'; $data = arr...

mysql批量添加数据

$conn=new sqlhelper;for ($i = 0; $i <1000 ; $i++) {$sql="insert into system_user(user_shenfe...

短地址生成,全套配置教程

短地址生成,全套配置教程

nginx 配置location / {rewrite  ^(.*)$  /index.php?s=/$1  last;}code rotoo.top.7z...

php数组元素的增加删除修改操作方法

增加$arr[$iy]["pr_count"]+=$pr_count;减少$arr[$iy]["pr_count"]-=$pr_count;删除unset($a...

php字符截取

substr(变量, 0,506)...

MySQL 及 SQL 注入

所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。我们永远不要信任用户的输入,我们必须认定用户输入的数据都是不安全的,我...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。