如何访问对象中数组中对象的值? [重复]

时间:2022-04-13 23:54:46

This question already has an answer here:

这个问题在这里已有答案:

I'm trying to work with some web service's API. It returns this massive object. What I want to know is how to work with it such that I can say "The cost of [shiptypename] is [costfee]" for each of the elements.

我正在尝试使用一些Web服务的API。它返回这个庞大的对象。我想知道的是如何使用它,以便我可以为每个元素说“[shiptypename]的成本是[costfee]”。

I tried reading tutorials on objects and found various answers here on stack that seemed almost right, but I'm lost. I gather it starts with:

我尝试阅读有关对象的教程,并在堆栈上找到了各种答案,看起来几乎是正确的,但我迷路了。我收集它开始于:

 $result["rates"]->

but everything I've tried appending to that gives me errors :(

但是我尝试过的所有内容都给了我错误:(

Here's the object:

这是对象:

stdClass Object
(
    [ask] => Success
    [message] => Success.
    [time] => 2015-10-19 14:36:12
    [rates] => Array
        (
            [0] => stdClass Object
                (
                    [totalfee] => 11.86
                    [costfee] => 11.86
                    [dealfee] => 2
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 7-20
                    [isweight] => NO
                    [iftracking] => NO
                    [classtype] => China Post
                    [classtypecode] => CPS
                    [shiptype] => WWAM
                    [shiptypename] => WWAM
                    [shiptypecnname] => 中国邮政外围�包(平邮)
                )

            [1] => stdClass Object
                (
                    [totalfee] => 11.86
                    [costfee] => 11.86
                    [dealfee] => 2
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 7-20
                    [isweight] => NO
                    [iftracking] => NO
                    [classtype] => China Post
                    [classtypecode] => CPS
                    [shiptype] => CNQM
                    [shiptypename] => CNQM
                    [shiptypecnname] => ��快邮�包(平邮)
                )

            [2] => stdClass Object
                (
                    [totalfee] => 12.94
                    [costfee] => 12.94
                    [dealfee] => 2
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 7-20
                    [isweight] => NO
                    [iftracking] => NO
                    [classtype] => China Post
                    [classtypecode] => CPS
                    [shiptype] => CNAM
                    [shiptypename] => Air Mail by China Post
                    [shiptypecnname] => 中国邮政�包(平邮)
                )

            [3] => stdClass Object
                (
                    [totalfee] => 14
                    [costfee] => 14
                    [dealfee] => 2
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 7-20
                    [isweight] => NO
                    [iftracking] => NO
                    [classtype] => China Post
                    [classtypecode] => CPS
                    [shiptype] => HKBAM
                    [shiptypename] => Bulk Air Mail by Hongkong Post
                    [shiptypecnname] => 香港邮政�包(平邮)
                )

            [4] => stdClass Object
                (
                    [totalfee] => 14.6
                    [costfee] => 14.6
                    [dealfee] => 2
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 5-15
                    [isweight] => NO
                    [iftracking] => NO
                    [classtype] => China Post
                    [classtypecode] => CPS
                    [shiptype] => SWBAM
                    [shiptypename] => Bulk Air Mail by Swiss Post
                    [shiptypecnname] => 瑞士邮政�包(平邮)
                )

            [5] => stdClass Object
                (
                    [totalfee] => 17.94
                    [costfee] => 17.94
                    [dealfee] => 0
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 6-10
                    [isweight] => NO
                    [iftracking] => NO
                    [classtype] => China Post
                    [classtypecode] => CPS
                    [shiptype] => DEAM2
                    [shiptypename] => Air Mail by Deutsch Post
                    [shiptypecnname] => 德国邮政�包(平邮)
                )

            [6] => stdClass Object
                (
                    [totalfee] => 20.5
                    [costfee] => 20.5
                    [dealfee] => 2
                    [regfee] => 8
                    [addons] => 0
                    [deliverytime] => 7-20
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Registered Postal
                    [classtypecode] => CRP
                    [shiptype] => WWRAM
                    [shiptypename] => WWRAM
                    [shiptypecnname] => 中国邮政外围�包(挂�)
                )

            [7] => stdClass Object
                (
                    [totalfee] => 20.5
                    [costfee] => 20.5
                    [dealfee] => 2
                    [regfee] => 8
                    [addons] => 0
                    [deliverytime] => 7-20
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Registered Postal
                    [classtypecode] => CRP
                    [shiptype] => CNQMR
                    [shiptypename] => CNQMR
                    [shiptypecnname] => ��快邮�包(挂�)
                )

            [8] => stdClass Object
                (
                    [totalfee] => 21.65
                    [costfee] => 21.65
                    [dealfee] => 2
                    [regfee] => 8
                    [addons] => 0
                    [deliverytime] => 7-20
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Registered Postal
                    [classtypecode] => CRP
                    [shiptype] => CNRAM
                    [shiptypename] => Registered Air Mail by China Post
                    [shiptypecnname] => 中国邮政�包(挂�)
                )

            [9] => stdClass Object
                (
                    [totalfee] => 24.77
                    [costfee] => 24.77
                    [dealfee] => 2
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 8-15
                    [isweight] => NO
                    [iftracking] => NO
                    [classtype] => China Post
                    [classtypecode] => CPS
                    [shiptype] => SFCQM3
                    [shiptypename] => SFCQM3
                    [shiptypecnname] => 快邮三级�包(平邮)
                )

            [10] => stdClass Object
                (
                    [totalfee] => 29.76
                    [costfee] => 29.76
                    [dealfee] => 0
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 6-10
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Registered Postal
                    [classtypecode] => CRP
                    [shiptype] => DERAM2
                    [shiptypename] => Registered Air Mail by Deutsch Post
                    [shiptypecnname] => 德国邮政�包(挂�)
                )

            [11] => stdClass Object
                (
                    [totalfee] => 30.34
                    [costfee] => 30.34
                    [dealfee] => 2
                    [regfee] => 15
                    [addons] => 0
                    [deliverytime] => 5-15
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Registered Postal
                    [classtypecode] => CRP
                    [shiptype] => SWBRAM
                    [shiptypename] => Registered Bulk Air Mail by Swiss Post
                    [shiptypecnname] => 瑞士邮政�包(挂�)
                )

            [12] => stdClass Object
                (
                    [totalfee] => 31.5
                    [costfee] => 31.5
                    [dealfee] => 2
                    [regfee] => 15
                    [addons] => 0
                    [deliverytime] => 7-20
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Registered Postal
                    [classtypecode] => CRP
                    [shiptype] => HKBRAM
                    [shiptypename] => Registered Bulk Air Mail by Hongkong Post
                    [shiptypecnname] => 香港邮政�包(挂�)
                )

            [13] => stdClass Object
                (
                    [totalfee] => 32.51
                    [costfee] => 32.51
                    [dealfee] => 0
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 7.0-10.0
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => USEXP
                    [shiptypename] => US and CA Express Line
                    [shiptypecnname] => ç¾ŽåŠ ä¸“çº¿
                )

            [14] => stdClass Object
                (
                    [totalfee] => 35.43
                    [costfee] => 35.43
                    [dealfee] => 2
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 8-15
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Registered Postal
                    [classtypecode] => CRP
                    [shiptype] => SFCQM3R
                    [shiptypename] => SFCQM3R
                    [shiptypecnname] => 快邮三级�包(挂�)
                )

            [15] => stdClass Object
                (
                    [totalfee] => 38.79
                    [costfee] => 38.79
                    [dealfee] => 2
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 5-15
                    [isweight] => NO
                    [iftracking] => NO
                    [classtype] => China Post
                    [classtypecode] => CPS
                    [shiptype] => SFCQM1
                    [shiptypename] => Air Mail by UK Post
                    [shiptypecnname] => 快邮�包一级平邮
                )

            [16] => stdClass Object
                (
                    [totalfee] => 102.18
                    [costfee] => 102.18
                    [dealfee] => 2
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 5-15
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Registered Postal
                    [classtypecode] => CRP
                    [shiptype] => SFCQM1R
                    [shiptypename] => Registered Air Mail by UK Post
                    [shiptypecnname] => 快邮�包一级挂�
                )

            [17] => stdClass Object
                (
                    [totalfee] => 122.01
                    [costfee] => 122.01
                    [dealfee] => 9.95
                    [regfee] => 0
                    [addons] => 8.06
                    [deliverytime] => 2-4
                    [isweight] => YES
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => CNUPS
                    [shiptypename] => UPS in China
                    [shiptypecnname] => 中国UPS
                )

            [18] => stdClass Object
                (
                    [totalfee] => 150.57
                    [costfee] => 150.57
                    [dealfee] => 9.95
                    [regfee] => 0
                    [addons] => 12.2
                    [deliverytime] => 2-7
                    [isweight] => YES
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => HKDHL1
                    [shiptypename] => DHL1 in HongKong
                    [shiptypecnname] => 香港DHL1
                )

            [19] => stdClass Object
                (
                    [totalfee] => 156.14
                    [costfee] => 156.14
                    [dealfee] => 9.95
                    [regfee] => 0
                    [addons] => 12.68
                    [deliverytime] => 2-7
                    [isweight] => YES
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => HKDHL
                    [shiptypename] => DHL in HongKong
                    [shiptypecnname] => 香港DHL
                )

            [20] => stdClass Object
                (
                    [totalfee] => 170.68
                    [costfee] => 170.68
                    [dealfee] => 9.95
                    [regfee] => 0
                    [addons] => 11.21
                    [deliverytime] => 4-6
                    [isweight] => YES
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => CNSFEDEX
                    [shiptypename] => Fedex International Economy Service in China
                    [shiptypecnname] => 中国FedEx�济型
                )

            [21] => stdClass Object
                (
                    [totalfee] => 177.95
                    [costfee] => 177.95
                    [dealfee] => 9.95
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 3-8
                    [isweight] => YES
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => SZEMS
                    [shiptypename] => Express Mail Service by Shenzhen Branch Post
                    [shiptypecnname] => 中国EMS
                )

            [22] => stdClass Object
                (
                    [totalfee] => 180.6
                    [costfee] => 180.6
                    [dealfee] => 0
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 2.0-7.0
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => HKEMS
                    [shiptypename] => Express Mail Service by * Post
                    [shiptypecnname] => 香港EMS
                )

            [23] => stdClass Object
                (
                    [totalfee] => 185.27
                    [costfee] => 185.27
                    [dealfee] => 9.95
                    [regfee] => 0
                    [addons] => 12.23
                    [deliverytime] => 2-4
                    [isweight] => YES
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => CNFEDEX
                    [shiptypename] => Fedex International Priority Service in China
                    [shiptypecnname] => 中国FedEx优先型
                )

            [24] => stdClass Object
                (
                    [totalfee] => 197.68
                    [costfee] => 197.68
                    [dealfee] => 9.95
                    [regfee] => 0
                    [addons] => 0
                    [deliverytime] => 4-10
                    [isweight] => NO
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => SGEMS
                    [shiptypename] => Express Mail Service by Singapore Post
                    [shiptypecnname] => æ–°åŠ å?¡EMS
                )

            [25] => stdClass Object
                (
                    [totalfee] => 201.15
                    [costfee] => 201.15
                    [dealfee] => 9.95
                    [regfee] => 0
                    [addons] => 13.75
                    [deliverytime] => 2-5
                    [isweight] => YES
                    [iftracking] => YES
                    [classtype] => China Express Mail
                    [classtypecode] => CRE
                    [shiptype] => HKUPS
                    [shiptypename] => UPS in Hongkong
                    [shiptypecnname] => 香港UPS
                )

        )

)

Addressing assertion this is a duplicate: the is specifically because I'm getting lost in the LEVEL. The Q you say is a dupe is an Array. Sorry if I'm confused, but that's WHY I'm asking!

解决断言这是一个重复:特别是因为我迷失了LEVEL。你说的Q是一个骗局是一个数组。对不起,如果我很困惑,但这就是我要问的原因!

1 个解决方案

#1


0  

Use this to iterate over the object:

使用它来迭代对象:

foreach ($object->rates as $rate) {
    print 'The cost of '. $rate->shiptypename . ' is ' . $rate->costfee . PHP_EOL;
}

#1


0  

Use this to iterate over the object:

使用它来迭代对象:

foreach ($object->rates as $rate) {
    print 'The cost of '. $rate->shiptypename . ' is ' . $rate->costfee . PHP_EOL;
}