网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 PHP Fatal error: Cannot use object of type stdClass as array in错误
内容
    下面一起来看看在php开发中碰到PHP Fatal error: Cannot use object of type stdClass as array in错误问题的解决办法吧。
    普通的数组出现如下错误
    代码如下:
    <?php
    Array (
    [0] => stdClass Object (
    [id] => 1
    [title] =>精彩推荐
    [size] => 280*150
    [pic] => ./uploadfiles/201402160422.jpg
    [state] => 0 )
    [1] => stdClass Object (
    [id] => 2
    [title] =>企业要闻
    [size] => 280*150
    [pic] => ./uploadfiles/201402160533.jpg
    [state] => 0 )
    )
    ?>
    后来百度搜索一个关于差不多的问题,但对方是json数据
    php再调用json_decode从字符串对象生成json对象时,如果使用[]操作符取数据,会得到下面的错误:
    Cannot use object of type stdClass as array
    产生原因:
    代码如下:
    $res = json_decode($res);
    $res['key']; //把 json_decode() 后的对象当作数组使用。
    解决方法(2种):
    1、使用 json_decode($d, true)。就是使json_decode 的第二个变量设置为 true。
    2、json_decode($res) 返回的是一个对象, 不可以使用 $res['key'] 进行访问, 换成 $res->key 就可以了。
    好了现在回到我们原问题发现
    原来是不能直接用[]来显示导致的,上面代码的输出是用的:$pic[0][title],改为$pic[0]->title后正常了
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/16 3:36:40