0

I have a dynamic number of multidimensional arrays, named like $market1, $market2, $market3... These arrays are formed (simplexml_load_file()) from XML files which have the following structure:

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


                              )

                            )
[...]

In every array there are several itemids, and I want to read out the values of specific ones. Something like this does not work (and I didn't expect it to)

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

I could solve it over an ugly foreach() construct I guess, but there has to be a better way. What is the best (fastest, elegant) way to give out the value under that specific market and that specific itemid?

John Alba
  • 265
  • 2
  • 4
  • 14
  • Try like `$market1[marketstat][itemid][buy][max]` because its not an object to use `$market1->marketstat->itemid->buy->max`. – Shashikumar Misal Apr 20 '15 at 10:43
  • i underinformed about the exact structure of the arrays, I tried it this way before, but as you can see at the now posted print_r output of one array, its not that easy as I would wish it to be. – John Alba Apr 20 '15 at 11:20
  • Now the object structure is too complex just convert it to array using `$array = (array) $yourObject;` & then try the previous suggestion. – Shashikumar Misal Apr 20 '15 at 11:31
  • Your question is very confused - you ask about multi-dimensional arrays when PHP does not have multi-dimensional arrays (it does have nested arrays) then the body of your question relates to an object instance - there are similarities between objects and arrays, but there are differences too. You refer to 'items' in your text but there is no entity named item or items in your dump. – symcbean Apr 20 '15 at 12:15
  • now i get it! thank you mate! sorry for the confusing question. the items are the ids in the dump. – John Alba Apr 20 '15 at 12:57
  • obviously I had problems with the difference between objects and arrays. – John Alba Apr 20 '15 at 13:13
  • @Jasper Jarlsen; Please leave an answer below and accept it later on if you have solved this. This will keep the information for future readers and you can also come back later and review the problems you once ran into ;) (this is totally accepted and you're even encouraged to do so just in case you wonder) – hakre Apr 20 '15 at 21:29
  • And if you have not found an answer so far: From your question I have problems to spot what you mean by arrays. You wrote you confused it (and it's easy to confuse with print_r or var_dump on a SimpleXMLElement because those functions will lie to you). Re-reading your question today you might be looking for [SimpleXML: Selecting Elements Which Have A Certain Attribute Value](http://stackoverflow.com/q/992450/367456). I still don't understand what you mean by ` $market1` to ` $market3`. It should be possible to put the whole data into one document, shouldn't it? – hakre Apr 25 '15 at 12:43

0 Answers0