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

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

问题描述

我正在使用以下代码从使用 Soap 的网站获取数据.

$client = new SoapClient('http://some.url.here');类 SMSParam {公共 $CellNumber;公共 $AccountKey;公共 $MessageCount;公共 $MessageBody;公共 $Reference;}$parameters = 新的 SMSParam;$参数 ->AccountKey = "$sms_key";$参数 ->MessageCount = "25";$Result = $client->GetIncomingMessages($parameters);echo "

";打印_r($结果);echo "</pre>";

以下是输出示例:

stdClass 对象([GetIncomingMessagesResult] =>标准类对象([SMSIncomingMessage] =>大批([0] =>标准类对象([传出消息ID] =>数据[参考] =>数据[消息编号] =>数据[电话号码] =>数据[消息] =>数据[接收日期] =>数据)[1] =>标准类对象([传出消息ID] =>数据[参考] =>数据[消息编号] =>数据[电话号码] =>数据[消息] =>数据[接收日期] =>数据)[2] =>标准类对象([传出消息ID] =>数据[参考] =>数据[消息编号] =>数据[电话号码] =>数据[消息] =>数据[接收日期] =>数据))))

如果只返回 1 个结果,我可以简单地执行以下操作:

$reference = $result->GetIncomingMessagesResult->SMSIncomingMessage->Reference;

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

任何帮助将不胜感激.

解决方案

它是一个数组,因此您可以使用 foreach 轻松地遍历它:

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

但是值得注意的是,默认情况下,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天全站免登陆