0

Using the below JSON fragment as an example, if I wanted to access the Value of Year, I would use something like

$result->ItemSpecifics->NameValueList[1]->Value[0] . 

However, this only works if you know the index of the Year array element (1 in this case).

My question is, if I don't know what the array index of Year is, how can I still access it' value? Is there a way to find the Year element by name rather than index?

This is not correct but I would expect a solution like:

$result->ItemSpecifics->NameValueList['Year']->Value[0]

JSON Example:

[ItemSpecifics] => stdClass Object
                (
                    [NameValueList] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [Name] => Returns Accepted
                                    [Value] => Array
                                        (
                                            [0] => ReturnsNotAccepted
                                        )

                                )

                            [1] => stdClass Object
                                (
                                    [Name] => Year
                                    [Value] => Array
                                        (
                                            [0] => 2001
                                        )

                                )

                            [2] => stdClass Object
                                (
                                    [Name] => Manufacturer
                                    [Value] => Array
                                        (
                                            [0] => Porsche
                                        )

                                )
  • Are you going to be accessing several items or just the 1 (the different is for just the 1 it would be quicker with a loop, more than one may be better to convert it to an indexed array). – Nigel Ren Jun 19 '20 at 11:09
  • I need to access more than 1 – George Spyrou Jun 19 '20 at 11:12
  • This post here answers the quetion: https://stackoverflow.com/questions/4742903/php-find-entry-by-object-property-from-an-array-of-objects – George Spyrou Jun 20 '20 at 16:32

0 Answers0