PHP:数组在 foreach 中行为异常
我有一个通过 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
如果我没记错的话,你的处理方式是错误的。
$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 themResourceID
.The
foreach
is running through every property of that object. Those properties will not have a property namedResourceID
.It looks like what you should be doing is drop the
foreach
and address$noteso->ResourceID
.