stdClass对象和foreach循环 [英] stdClass object and foreach loops

查看:130
本文介绍了stdClass对象和foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码从使用Soap的网站获取数据。

  $ client = new SoapClient('http://some.url.here'); 
class SMSParam {
public $ CellNumber;
public $ AccountKey;
public $ MessageCount;
public $ MessageBody;
public $参考;

}
$参数=新的SMSParam;
$参数 - > AccountKey =$ sms_key;
$参数 - > MessageCount =25;
$ Result = $ client-> GetIncomingMessages($ parameters);
echo< pre>;
print_r($ Result);
echo< / pre>;

以下是一个输出示例:

< (
[SMSIncomingMessage] =>

[GetIncomingMessagesResult] => stdClass对象

[0] => stdClass Object

[OutgoingMessageID] => data
[Reference] => data
[MessageNumber] =>数据
[PhoneNumber] => data
[Message] => data
[ReceivedDate] => data


[1] => stdClass Object

[OutgoingMessageID] => data
[Reference] => data
[MessageNumber] => data
[PhoneNumber] =>数据
[Message] =>数据
[ReceivedDate] =>数据


[2] => stdClass Object

[OutgoingMessageID] => data
[Reference] => data
[MessageNumber] => data
[PhoneNumber] =>数据
[消息] =>数据
[ReceivedDate] =>数据







如果只返回1个结果,我可以简单地做这样的事情: / p>

  $ reference = $ result-> GetIncomingMessagesResult-> SMSIncomingMessage->引用; 

那么我该如何处理多个结果呢?



任何帮助将不胜感激。

解决方案

这是一个数组,所以你可以轻松地循环使用 foreach

  foreach($ result-> GetIncomingMessagesResult-> SMSIncomingMessage作为$ message){
echo $ message->引用;

$ / code>

不过值得注意的是PHP的 SoapClient 默认情况下,只有当数组中有多个值时,才会将数组作为一个PHP数组返回 - 如果只有一个值,您将获得该值(不包含在数组中)。一个简单的方法是在 SoapClient 构造函数中使用 SOAP_SINGLE_ELEMENT_ARRAYS 选项;这将防止这种行为,并确保你总是得到数组。


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.

解决方案

It is an array, so you can loop over it easily using foreach:

foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message) {
    echo $message->Reference;
}

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 option SOAP_SINGLE_ELEMENT_ARRAYS in the SoapClient constructor; this will prevent this behaviour and ensure you always get arrays.

这篇关于stdClass对象和foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆