duanrui3480 2017-07-14 13:01
浏览 98
已采纳

如何在php中访问stdclass对象?

This is my object:

$reportdata = stdClass Object
(
    [1] => stdClass Object
        (
            [reportname] => reportname1
            [reportviews] => 20
            [reportpath] => reports/reportname1
            [thumbnailurl] => reports/thumbnailurl/thumbnailurl1.jpg
            [description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        )

    [2] => stdClass Object
        (
            [reportname] => reportname2
            [reportviews] => 20
            [reportpath] => reports/reportname2
            [thumbnailurl] => reports/thumbnailurl/thumbnailurl2.jpg
            [description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        )


)

I am trying to access the individual values using $reportdata[1]['reportviews'] but I get an error

"Cannot use object of type stdClass as array".

How can I access reportname and reportviews?

  • 写回答

5条回答 默认 最新

  • duandian4501 2017-07-14 13:04
    关注

    Try like this

     $reportdata=json_decode(json_encode($reportdata),true);
    

    then use this

    $reportdata[1]['reportviews']
    

    You will not get any error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开