PHP从JSON获取特定数据

时间:2018-01-17 21:50:34

标签: php json

我尝试从此处获取icons -> icon值:

{"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}

使用此:

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

但它似乎没有返回正确的值,它返回的全部是null。我已经尝试过$tmdb['icons']->icon以及某些教程中的建议,但这似乎也没有做到这一点。

有谁知道出了什么问题?

1 个答案:

答案 0 :(得分:3)

您需要使用(如果on branch ...的第二个参数为json_decode())来访问它:

true

你可以看到:

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

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

如果[]的第二个参数未设置为json_decode(),您可以像这样访问它:

true

这假设你已经解码了你的JSON数据,如果你没有,你必须这样:

$tmdb->icons[0]->icon