php stdClass 对象和 foreach 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/950827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:25:36  来源:igfitidea点击:

stdClass object and foreach loops

phpobjectforeachstdclass

提问by jason_m

I am using the following code to get data from a website using Soap.

我正在使用以下代码从使用 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:

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

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

So how would I go about working with multiple results?

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

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by Tom Haigh

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

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

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

However it is worth noting that PHP's SoapClientby 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_ARRAYSin the SoapClientconstructor; this will prevent this behaviour and ensure you always get arrays.

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

回答by Peter Bailey

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.

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

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

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

回答by mdskinner

you need to specify your SMSIncomingMessage arrays object key.

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

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

or

或者

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

回答by Danilo Santos

Cast object to convert array

将对象转换为数组

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

回答by Philippe Gerber

Iterate over the array?! :-)

遍历数组?!:-)

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