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

admin4年前php935

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 就可以了。


相关文章

数组问题整理

$arr[]           //用于收集子数组;serialize($arr)&nb...

php设置16位的md5加密方法

用substr函数实现substr(md5(变量),8,16);就可以实现默认的md5(变量)输出的是32位加密方法...

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

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

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

php生成的json解决中文乱码的解决方案

echo json_encode($arr,JSON_UNESCAPED_UNICODE); //转换成json,并让中文不变成乱码JSON_UNESCAPED_UNICODE 这...

php post undefined index,PHP 中提示undefined index的解决方案

声明为空isset($_POST['user_email']),或者,empty($_POST['user_email'])在php.ini中设置error_repor...

mysql批量添加数据

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

发表评论    

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