0

I want to use PHP to parse JSON data from an external site. This code is working as intended:

$json = file_get_contents($url);
$obj = json_decode($json);

... and

echo $obj->title;

... is posting the title from the JSON data (Far Cry Primal):

{"id":241408,"title":"Far Cry Primal","url":"http:\/\/www.prisguide.no\/produkt\/far-cry-primal-241408","price":547,"priceCount":13,"image":{"100x100":"http:\/\/img.prisguide.no\/1678\/1678079\/original.77x100mt.jpg","300x300":"http:\/\/img.prisguide.no\/1678\/1678079\/original.232x300t.jpg","500x500":"http:\/\/img.prisguide.no\/1678\/1678079\/original.jpg"},"category":"Spill","categoryId":32,"manufacturer":"Ubisoft Montreal","specifications":"Xbox One, Action, 18+, Ubisoft","specificationsFull":[{"id":96,"name":"Plattform","type":"select","sectionName":"Generelt","value":[{"id":45275,"value":"Xbox One"}]}

But I need to access more elements, like the last one (scroll all the way to the right) - value: Xbox One.This has it's own ID, but im not sure how to access it.

Any hints?

5
  • 3
    I very much doubt this is working as intended, using json_decode($json, true) gives you an array, not an object. Commented Mar 14, 2016 at 19:32
  • you can use foreach of $obj->value and get the data easily Commented Mar 14, 2016 at 19:32
  • Sorry, removed "true", its working without that - but there is more data with the "value" attribute (every single one is bounded with an ID, but I didnt want to post miles of similar code) - and what I want is the value of the specific ID Commented Mar 14, 2016 at 19:36
  • Try $obj['specificationsFull']['value']['value']; Commented Mar 14, 2016 at 19:36
  • Also, your current json string is an invalid json. Commented Mar 14, 2016 at 19:39

1 Answer 1

1

Try something like this, your JSON isn't valid so I can't test it but I'm sure you get the idea.

foreach($obj as $items)
{
    foreach($items as $item)
    {
        foreach($item as $value)
        {
            foreach($value as $key => $value)
            {
                echo $key . ': ' . $value;
                echo '<br>';
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is very close! I just think I need to go one level "deeper", as its printing: value: Array and I need the content inside that array
I've updated the answer to include another level. Please accept this answer if you're happy it's helped :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.