stdClass 对象和 foreach 循环
我正在使用以下代码通过 Soap 从网站获取数据。
$client = new SoapClient('http://some.url.here');
class SMSParam {
public $CellNumber;
public $AccountKey;
public $MessageCount;
public $MessageBody;
public $Reference;
}
$parameters = new SMSParam;
$parameters -> AccountKey = "$sms_key";
$parameters -> MessageCount = "25";
$Result = $client->GetIncomingMessages($parameters);
echo "<pre>";
print_r($Result);
echo "</pre>";
以下是输出示例:
stdClass Object
(
[GetIncomingMessagesResult] => stdClass Object
(
[SMSIncomingMessage] => Array
(
[0] => stdClass Object
(
[OutgoingMessageID] => data
[Reference] => data
[MessageNumber] => data
[PhoneNumber] => data
[Message] => data
[ReceivedDate] => data
)
[1] => stdClass Object
(
[OutgoingMessageID] => data
[Reference] => data
[MessageNumber] => data
[PhoneNumber] => data
[Message] => data
[ReceivedDate] => data
)
[2] => stdClass Object
(
[OutgoingMessageID] => data
[Reference] => data
[MessageNumber] => data
[PhoneNumber] => data
[Message] => data
[ReceivedDate] => data
)
)
)
)
如果仅返回 1 个结果,我可以简单地执行如下操作:
$reference = $result->GetIncomingMessagesResult->SMSIncomingMessage->Reference;
那么我将如何处理多个结果呢?
任何帮助将不胜感激。
I am using the following code to get data from a website using Soap.
$client = new SoapClient('http://some.url.here');
class SMSParam {
public $CellNumber;
public $AccountKey;
public $MessageCount;
public $MessageBody;
public $Reference;
}
$parameters = new SMSParam;
$parameters -> AccountKey = "$sms_key";
$parameters -> MessageCount = "25";
$Result = $client->GetIncomingMessages($parameters);
echo "<pre>";
print_r($Result);
echo "</pre>";
Here is a sample of the output:
stdClass Object
(
[GetIncomingMessagesResult] => stdClass Object
(
[SMSIncomingMessage] => Array
(
[0] => stdClass Object
(
[OutgoingMessageID] => data
[Reference] => data
[MessageNumber] => data
[PhoneNumber] => data
[Message] => data
[ReceivedDate] => data
)
[1] => stdClass Object
(
[OutgoingMessageID] => data
[Reference] => data
[MessageNumber] => data
[PhoneNumber] => data
[Message] => data
[ReceivedDate] => data
)
[2] => stdClass Object
(
[OutgoingMessageID] => data
[Reference] => data
[MessageNumber] => data
[PhoneNumber] => data
[Message] => data
[ReceivedDate] => data
)
)
)
)
If only 1 result is returned, I can simply do something like this:
$reference = $result->GetIncomingMessagesResult->SMSIncomingMessage->Reference;
So how would I go about working with multiple results?
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的看法是始终确保您有一个消息数组,即使它是一个 1 的数组。这样您就不会重复任何处理。
My take on it is to just always make sure you have an array of messages, even if it's an array of 1. That way you don't duplicate any processing.
您需要指定 SMSIncomingMessage 数组对象键。
或者
you need to specify your SMSIncomingMessage arrays object key.
or
将对象转换为数组
Cast object to convert array
迭代数组?! :-)
Iterate over the array?! :-)
它是一个数组,因此您可以使用
foreach
轻松地循环它:但是值得注意的是,默认情况下,PHP 的
SoapClient
似乎仅在存在时才将数组作为 PHP 数组返回数组中有多个值 - 如果只有一个值,您将只得到该值(不包含在数组中)。 解决这个问题的一个简单方法是在SoapClient
构造函数中使用选项SOAP_SINGLE_ELEMENT_ARRAYS
; 这将防止这种行为并确保您始终获得数组。It is an array, so you can loop over it easily using
foreach
:However it is worth noting that PHP's
SoapClient
by default appears to return arrays as a PHP array only when there is more than one value in the array - if there is only one value you will just get that value (not contained within an array). An easy way around this is to use the optionSOAP_SINGLE_ELEMENT_ARRAYS
in theSoapClient
constructor; this will prevent this behaviour and ensure you always get arrays.