stdClass object and foreach loops
Asked Answered
R

5

17

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.

Reganregard answered 4/6, 2009 at 14:9 Comment(0)
F
41

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.

Forefinger answered 4/6, 2009 at 14:18 Comment(2)
Spent the day yesterday puzzled over this very thing. I had no idea about the "SOAP_SINGLE_ELEMENT_ARRAYS" flag; thanks for that.Semitropical
for people who don't know how to add the option : $client = new SoapClient('http://www.example.com', array('features' => SOAP_SINGLE_ELEMENT_ARRAYS,));Bagpipes
G
3

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;
}
Gainer answered 4/6, 2009 at 14:22 Comment(0)
B
2

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;
//...
}
Butcherbird answered 2/7, 2010 at 10:9 Comment(0)
B
1

Cast object to convert array

$array = (array) json_decode(['TEST'=>true]);
Barracoon answered 29/11, 2019 at 2:20 Comment(0)
U
0

Iterate over the array?! :-)

foreach ($result->GetIncomingMessagesResult->SMSIncomingMessage as $message)
{
    $reference = $message->Reference;
    //...
}
Undervest answered 4/6, 2009 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.