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

如何在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 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件