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

admin5年前php1066

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 post undefined index,PHP 中提示undefined index的解决方案

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

在apache怎么常量 变量

在httpd.conf添加<IfModule mod_env.c>SetEnv 常量名 常量值</IfModule>...

把字符按照逗号分割成数组

 <?php $cars=$_POST['shuju'];$cars=explode(',', $cars);print_r($cars)&n...

php替换函数--数组写法

<?php  $my_name='&&&&'; //echo  substr(xml_entities(...

mysql批量添加数据

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

php.ini 7.3设置

date.timezone = PRCdisplay_errors = On网络保存图片,以及微信获取手机号码: windows下的PHP,只需要到php.ini中把前面的&nb...

发表评论    

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