PHP:数组在 foreach 中行为异常

发布于 09-29 16:14 字数 2711 浏览 7 评论 0原文

我有一个通过 SOAP 客户端查询数据的类。查询结果作为对象数组返回。一些奇怪的事情正在发生。我不确定它是否仅在数组中有一个元素时发生。

这是代码:

function getNotes($ticketid){

    $ticketid = $this->ms_escape_string($ticketid);

    $conn = new Connections();
    $client = $conn->connectToAPI();

    $xml = array('sXML' =>"<queryxml>".
                                "<entity>TimeEntry</entity>".
                                "<query>".
                                    "<field>TicketID".
                                        "<expression op='equals'>$ticketid</expression>".
                                    "</field>".
                                "</query>".
                        "</queryxml>");

    $result = $client->query($xml);

    $noteso = $result->queryResult->EntityResults->Entity;

    //set array counter
    $x=0;


    var_dump($noteso);

        foreach($noteso as $note){

            var_dump($note->ResourceID);

            $name = $this->getResourceName($note->ResourceID);


            $timeentryarray[$x] = array('createdate'     => $note->CreateDateTime,
                            'summarynotes'  => $note->SummaryNotes,
                            'resource'    => $name
                           );
            $x++;

        }

        return $timeentryarray;
}

当应用程序运行时 var_dump($noteso) 就可以了。打印出来,

object(stdClass)#12 (15) {
    ["id"]=> int(36988)
    ["UserDefinedFields"]=> object(stdClass)#13 (0) { } 
    ["TicketID"]=> int(26390) 
    ["InternalAllocationCodeID"]=> int(28303142) 
    ["Date"]=> string(19) "2010-10-25T00:00:00" 
    ["StartDateTime"]=> string(19) "2010-10-25T04:59:00" 
    ["EndDateTime"]=> string(19) "2010-10-25T05:04:00" 
    ["HoursWorked"]=> float(0.08)
    ["HoursToBill"]=> float(0.0833333358) 
    ["OffsetHours"]=> float(0)
    ["SummaryNotes"]=> string(69) "Hi Sarah, Do you have an example of a bounced email? Regards Simon" 
    ["InternalNotes"]=> string(0) "" 
    ["RoleID"]=> int(24482927) 
    ["CreateDateTime"]=> string(23) "2010-10-25T05:06:28.023" 
    ["ResourceID"]=> int(29699970) 
} 

但 foreach 循环中的第二个 var_dump($note->ResourceID) 只显示

NULL

之后访问数组的每一行,然后抛出错误:

Notice: Trying to get property of non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ClientPortal\classes\OpenTickets.php on line 204

第 204 行 一直在

$name = $this->getResourceName($note->ResourceID);

尝试调试此行一段时间,有人可以提供任何建议?

问候,

琼斯

I have a class that is query data via a SOAP client. the results of the query are returned as an array of objects. Something strange is happening. I'm not sure if it only occurs when there is one element in the array.

Here is the code:

function getNotes($ticketid){

    $ticketid = $this->ms_escape_string($ticketid);

    $conn = new Connections();
    $client = $conn->connectToAPI();

    $xml = array('sXML' =>"<queryxml>".
                                "<entity>TimeEntry</entity>".
                                "<query>".
                                    "<field>TicketID".
                                        "<expression op='equals'>$ticketid</expression>".
                                    "</field>".
                                "</query>".
                        "</queryxml>");

    $result = $client->query($xml);

    $noteso = $result->queryResult->EntityResults->Entity;

    //set array counter
    $x=0;


    var_dump($noteso);

        foreach($noteso as $note){

            var_dump($note->ResourceID);

            $name = $this->getResourceName($note->ResourceID);


            $timeentryarray[$x] = array('createdate'     => $note->CreateDateTime,
                            'summarynotes'  => $note->SummaryNotes,
                            'resource'    => $name
                           );
            $x++;

        }

        return $timeentryarray;
}

when the application run the var_dump($noteso) is fine. Prints out

object(stdClass)#12 (15) {
    ["id"]=> int(36988)
    ["UserDefinedFields"]=> object(stdClass)#13 (0) { } 
    ["TicketID"]=> int(26390) 
    ["InternalAllocationCodeID"]=> int(28303142) 
    ["Date"]=> string(19) "2010-10-25T00:00:00" 
    ["StartDateTime"]=> string(19) "2010-10-25T04:59:00" 
    ["EndDateTime"]=> string(19) "2010-10-25T05:04:00" 
    ["HoursWorked"]=> float(0.08)
    ["HoursToBill"]=> float(0.0833333358) 
    ["OffsetHours"]=> float(0)
    ["SummaryNotes"]=> string(69) "Hi Sarah, Do you have an example of a bounced email? Regards Simon" 
    ["InternalNotes"]=> string(0) "" 
    ["RoleID"]=> int(24482927) 
    ["CreateDateTime"]=> string(23) "2010-10-25T05:06:28.023" 
    ["ResourceID"]=> int(29699970) 
} 

but the second var_dump($note->ResourceID) in the foreach loop only shows

NULL

then each line that accesses the array after that throws an error:

Notice: Trying to get property of non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ClientPortal\classes\OpenTickets.php on line 204

Line 204 being

$name = $this->getResourceName($note->ResourceID);

Been trying to debug this one for a while, can someone offer any suggestions?

Regards,

Jonesy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍景流年2024-10-06 16:14:02

如果我没记错的话,你的处理方式是错误的。 $noteso 有许多属性,其中包括 ResourceID

foreach 正在遍历该对象的每个属性。这些属性不会有名为 ResourceID 的属性。

看起来您应该做的是删除 foreach 和地址 $noteso->ResourceID

If I'm not mistaken, you are going about this the wrong way. $noteso has a number of properties, among them ResourceID.

The foreach is running through every property of that object. Those properties will not have a property named ResourceID.

It looks like what you should be doing is drop the foreach and address $noteso->ResourceID.

樱桃奶球2024-10-06 16:14:02

看起来 $noteso = $result->queryResult->EntityResults->Entity; 返回单个对象,

也许需要 while 循环,您使用什么肥皂客户端?

It looks like $noteso = $result->queryResult->EntityResults->Entity; is return a single object,

Maybe a while loop is needed, what soap-client are you using ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文