从多维数组中取值

时间:2015-04-20 10:36:03

标签: php arrays xml multidimensional-array

我有一个动态数量的多维数组,命名为$ market1,$ market2,$ market3 ...... 这些数组由XML文件组成(simplexml_load_file()),具有以下结构:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
            [method] => marketstat_xml
        )

    [marketstat] => SimpleXMLElement Object
        (
            [type] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [id] => 25268
                            )

                        [buy] => SimpleXMLElement Object
                            (
                                [volume] => 68049
                                [avg] => 79171.90
                                [max] => 85251.27
                                [min] => 71005.66
                                [stddev] => 5187.52
                                [median] => 80030.04
                                [percentile] => 85250.46
                            )

                        [sell] => SimpleXMLElement Object
                            (
                                [volume] => 13052
                                [avg] => 407071.42
                                [max] => 79000000.00
                                [min] => 98000.00
                                [stddev] => 27607034.09
                                [median] => 104990.00
                                [percentile] => 104689.17
                            )

                        [all] => SimpleXMLElement Object
                            (
                                [volume] => 91051
                                [avg] => 74144.16
                                [max] => 120000.00
                                [min] => 23.50
                                [stddev] => 19857.57
                                [median] => 83351.92
                                [percentile] => 23.50
                            )

                    )

                [1] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [id] => 28694
                            )

                        [buy] => SimpleXMLElement Object
                            (
                                [volume] => 285000
                                [avg] => 1610.29
                                [max] => 2011.64
                                [min] => 801.01
                                [stddev] => 386.84
                                [median] => 1824.15
                                [percentile] => 2011.64
                            )

                        [sell] => SimpleXMLElement Object
                            (
                                [volume] => 53287
                                [avg] => 4560.18
                                [max] => 9998.00
                                [min] => 3811.63
                                [stddev] => 2446.09
                                [median] => 4189.46
                                [percentile] => 3811.63
                            )

                        [all] => SimpleXMLElement Object
                            (
                                [volume] => 343309
                                [avg] => 1912.47
                                [max] => 4798.00
                                [min] => 23.50
                                [stddev] => 1340.91
                                [median] => 2011.55
                                [percentile] => 671.43


                              )

                            )
[...]

在每个数组中都有几个itemid,我想读出特定值的值。 像这样的东西不起作用(我没想到它)

print   ${"market".$number}->marketstat->$id->buy->max;

我可以通过一个丑陋的foreach()构造来解决它,但我必须有更好的方法。 什么是在特定市场和具体项目下给出价值的最佳(最快,最优雅)方式?

0 个答案:

没有答案