开发者

PHP: Array acting strangely in foreach

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


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.


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 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜