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

admin5年前php1075

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过滤emod与特殊字符

<?php  function xml_entities($string) { //过滤emod $ts_zifu= json_encode($st...

php设置跨站访问

  php的open_basedir设置多个路径(目录),分隔符是“:”冒号open_basedir=/www/wwwroot/www.ceshi.com/:/tmp/:/www/...

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

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

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

mysql批量添加数据

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

数组问题整理

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

php的function函数内部只能有一个return

php的function函数内部只能有一个return,之后的代码就都全部运行不正常了...

发表评论    

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