tags:

views:

483

answers:

3

I've seen similar questions on here but I can't seem to apply the solutions to my problem. I have a variable called $results which I got from an API. I'll change the proper nouns so as to protect my work's customers:

stdClass Object
(
    [out] => stdClass Object
        (
            [count] => 2
            [transactions] => stdClass Object
                (
                    [RealTimeCommissionDataV2] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [adId] => 12345678
                                    [advertiserId] => 123456789
                                    [advertiserName] => Chuck E. Cheese, inc.
                                    [commissionAmount] => 50
                                    [country] => US
                                    [details] => stdClass Object
                                        (
                                        )

                                    [eventDate] => 2009-11-16T09:44:25-08:00
                                    [orderId] => X-XXXXXXXXXX
                                    [saleAmount] => 0
                                    [sid] => 123456789
                                    [websiteId] => 2211944
                                )

                            [1] => stdClass Object
                                (
                                    [adId] => 987654321
                                    [advertiserId] => 12345
                                    [advertiserName] => Chorizon Wireless.
                                    [commissionAmount] => 50
                                    [country] => US
                                    [details] => stdClass Object
                                        (
                                        )

                                    [eventDate] => 2009-11-16T09:58:40-08:00
                                    [orderId] => X-CXXXXXX
                                    [saleAmount] => 0
                                    [sid] => 61-122112
                                    [websiteId] => 1111922
                                )
                        )
                )
        )
)

I shortened it to two entries here but the number of entries will vary, it's the result of a check for transactions in the past hour, there may sometimes be only one and sometimes as many as a dozen.

I want to assign these entries to variables like websiteId1 websiteId2 etc. I know I need to do a foreach loop but can't seem to figure it out. How can I write it so that I get the "[details]" as well?

+2  A: 
foreach ($results->out->transactions->RealTimeCommissionDataV2 AS $commissionData) {
    // you can access the commissionData objects now, i.e.:
    $commissionData->adId;
    $commissionData->details;
}
cballou
thanks to you also!
pg
+3  A: 
<?
 foreach ($result->out->transactions->RealTimeCommissionDataV2 as $item)
 {
   // do somthing with each item.
   print_r($item);

   // or the details array
   $num_details  = sizeof($item->details)
 }
Byron Whitlock
+2  A: 

I think this is what you want.

EDIT

Updated based on some notes in the documentation. Specifically, these two

a numerically indexed array will not produce results unless you use EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID.

Prefixes are automatically separated from the array key by an underscore character.

echo extract( $results->out->transactions->RealTimeCommissionDataV2, EXTR_PREFIX_ALL, 'websiteId' );

// test the extract
print_r( $websiteId_0 );
Peter Bailey
thank you very much!
pg
This doesn't print anything, if I change it to "$websiteId" it echoes one proper value but the problem is that there will may be up to a dozen. I want to have $websiteID1 $websiteID2 $websiteID3 etc.
pg
Ok, I looked into the problem and updated my answer.
Peter Bailey