dsj0312 2013-02-26 13:07
浏览 41
已采纳

如何正确访问数组对象

I've a result from a webservice's query and I would like to get some values from it. It works but I have PHP notice issues, so I'm probably doing something wrong.

This is the $items variable content :

stdClass Object
(
    [response] => stdClass Object
        (
            [0] => stdClass Object
                (
                    [id] => 275
                    [corpid] => 16107
                    [name] => default
                    [description] => 
                    [status] => ok
                    [nbSteps] => 7
                )

            [defaultItem] => 275
        )

    [error] => 
    [status] => success
)

So I tried something like :

foreach ( $items->response AS $key => $item ) {
    if ( $item->name == 'default' ){ // Line 106
        $Id = $item->id;
    }
}

It works, $Id is equal to 275 but PHP returns a notice :

Notice: Trying to get property of non-object in /home/web/dev/webservice-form.php on line 106

Any help would be greatly appreciated.

EDIT : This is the content of the $item variable (taken from the foreach loop) :

stdClass Object
(
    [id] => 275
    [corpid] => 16107
    [name] => default
    [description] => 
    [status] => ok
    [nbSteps] => 7
)

275

Please note that the '275' is a part of the result.

  • 写回答

2条回答 默认 最新

  • duanpu1111 2013-02-26 13:10
    关注

    The problem is the defaultItem entry in your inner object. Your loop will at some point reach this and try to access name, which doesn't exist, because there is no object.

    Should be easily solveable with is_object().

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路