J. Doe
J. Doe

Reputation: 69

PHP Get Specific Data from JSON

I'm trying to fetch the icons -> icon value from this:

{"revision":5,"patchRevision":121,"formatVersion":4,"npTitleId":"CUSA00744_00","console":"PS4","names":[{"name":"Minecraft: PlayStation®4 Edition"}],"icons":[{"icon":"http://gs2-sec.ww.prod.dl.playstation.net/gs2-sec/appkgo/prod/CUSA00744_00/5/i_30fd62592fcf63ded20a048269062dff3113c438d32414b9da63dde6f3d86f7c/i/icon0.png","type":"512x512"}],"parentalLevel":4,"pronunciation":"http://gs2-sec.ww.prod.dl.playstation.net/gs2-sec/appkgo/prod/CUSA00744_00/5/i_30fd62592fcf63ded20a048269062dff3113c438d32414b9da63dde6f3d86f7c/i/pronunciation.xml","contentId":"UP4433-CUSA00744_00-MINECRAFTPS40000","backgroundImage":"http://gs2-sec.ww.prod.dl.playstation.net/gs2-sec/appkgo/prod/CUSA00744_00/5/i_30fd62592fcf63ded20a048269062dff3113c438d32414b9da63dde6f3d86f7c/i/pic0.png","bgm":"http://gs2-sec.ww.prod.dl.playstation.net/gs2-sec/appkgo/prod/CUSA00744_00/5/i_30fd62592fcf63ded20a048269062dff3113c438d32414b9da63dde6f3d86f7c/i/snd0.at9","category":"gd","psVr":0,"neoEnable":1}

Using this:

$tmdb['icons']['icon']

But it doesn't seem to return the right value, All it returns is null. I've tried doing $tmdb['icons']->icon aswell as suggested in some tutorials, but that didn't seem to do the trick either.

Does anyone know what's going wrong?

Upvotes: 0

Views: 161

Answers (1)

Spoody
Spoody

Reputation: 2882

You need to access it using (if the second parameter for json_decode() is true):

$tmdb['icons'][0]['icon']

as you can see:

"icons": [
    {
      "icon": "http://gs2-sec.ww.prod.dl.playstation.net/gs2-sec/appkgo/prod/CUSA00744_00/5/i_30fd62592fcf63ded20a048269062dff3113c438d32414b9da63dde6f3d86f7c/i/icon0.png",
      "type": "512x512"
    }
  ],

is an array, [] means an array

If json_decode()'s second parameter is not set to true, you can access it like this:

$tmdb->icons[0]->icon

This is assuming that you have already decoded your JSON data, if you didn't you have to like this:

$data = json_decode($your_json)

Upvotes: 3

Related Questions