stdClass 对象和 foreach 循环

发布于 2024-07-22 19:16:45 字数 2150 浏览 3 评论 0原文

我正在使用以下代码通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

血之狂魔 2024-07-29 19:16:46

我的看法是始终确保您有一个消息数组,即使它是一个 1 的数组。这样您就不会重复任何处理。

$smsMessages = is_array( $result->GetIncomingMessagesResult->SMSIncomingMessage )
    ? $result->GetIncomingMessagesResult->SMSIncomingMessage
    : array( $result->GetIncomingMessagesResult->SMSIncomingMessage );

foreach ( $smsMessages as $smsMessage )
{
    echo $smsMessage->Reference;
}

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.

$smsMessages = is_array( $result->GetIncomingMessagesResult->SMSIncomingMessage )
    ? $result->GetIncomingMessagesResult->SMSIncomingMessage
    : array( $result->GetIncomingMessagesResult->SMSIncomingMessage );

foreach ( $smsMessages as $smsMessage )
{
    echo $smsMessage->Reference;
}
北城挽邺 2024-07-29 19:16:46

您需要指定 SMSIncomingMessage 数组对象键。

$result->GetIncomingMessagesResult->SMSIncomingMessage[0]->Reference;

或者

foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message)
{
$reference = $message[0]->Reference;
//...
}

you need to specify your SMSIncomingMessage arrays object key.

$result->GetIncomingMessagesResult->SMSIncomingMessage[0]->Reference;

or

foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message)
{
$reference = $message[0]->Reference;
//...
}
梦里寻她 2024-07-29 19:16:46

将对象转换为数组

$array = (array) json_decode(['TEST'=>true]);

Cast object to convert array

$array = (array) json_decode(['TEST'=>true]);
岁月染过的梦 2024-07-29 19:16:46

迭代数组?! :-)

foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message)
{
    $reference = $message->Reference;
    //...
}

Iterate over the array?! :-)

foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message)
{
    $reference = $message->Reference;
    //...
}
撕心裂肺的伤痛 2024-07-29 19:16:45

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文