English 中文(简体)
foreach loops & stdclass objects
原标题:

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?

最佳回答
foreach ($results->out->transactions->RealTimeCommissionDataV2 AS $commissionData) {
    // you can access the commissionData objects now, i.e.:
    $commissionData->adId;
    $commissionData->details;
}
问题回答
<?
    foreach ($result->out->transactions->RealTimeCommissionDataV2 as $item)
    {
            // do somthing with each item.
            print_r($item);

            // or the details array
            $num_details  = sizeof($item->details)
    }

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 );




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签