foreach循环& stdclass对象 [英] foreach loops & stdclass objects

查看:132
本文介绍了foreach循环& stdclass对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到过类似的问题,但似乎无法将解决方案应用于我的问题。我有一个变量称为$结果,我从API获得。我会改变专有名词,以保护我的工作客户:

  stdClass对象

[out] => stdClass Object

[count] => 2
[transactions] => stdClass Object

[RealTimeCommissionDataV2] => ;数组

[0] => stdClass对象

[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] => Chorizo​​n 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





我将其缩短为两个条目,的条目会有所不同,这是在过去一小时交易检查的结果,有时可能只有一个,有时多达一打。

我想将这些条目分配给诸如websiteId1 websiteId2等变量。我知道我需要做一个foreach循环,但似乎无法弄清楚。我怎样才能写出来,让我也能得到[details]?

解决方案

  foreach($ results-> out-> transactions-> RealTimeCommissionDataV2 AS $ commissionData){
//你现在可以访问这个commissionData对象,例如:
$ commissionData-> adId;
$ commissionData->详情;
}


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循环& stdclass对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆