我有一個json文件,我正在閱讀url的內容。我只想解析一個特定的對象/段,單獨存儲並顯示它。PHP json_decode解析特定對象/段
.Json
{
"playerstats": {
"id": "adwawdawdawdawd",
"game": "123324234",
"stats": [
{
"name": "tk",
"value": 57656
},
{
"name": "td",
"value": 38439
},
{
"name": "ttp",
"value": 2659434
},
{
"name": "tpb",
"value": 847
},
]
"achievements": [
{
"name": "wbp",
"achieved": 1
},
{
"name": "bpl",
"achieved": 1
},
{
"name": "bdl",
"achieved": 1
},
{
"name": "kel",
"achieved": 1
}
]
}
}
所以我想要一個單獨的「Stats」數組和一個單獨的「Achievements」數組。
然後我可以循環,並得到這樣
Stats: name, value
name, value
name, value
...
Achievements: name, achieved
name, achieved
name, achieved
...
的輸出。
.PHP Attempt
$url = "insertjsonUrlHere";
$jsonFile = file_get_contents($url);
$obj = json_decode($jsonFile, true);
$stats = $obj->playerstats->stats;
$achievements = $obj->playerstats->achievements;
foreach ($stats as $key => $value) {
echo $key." ".$value."<br />";
}
foreach ($achievements as $key => $value) {
echo $key." ".$value."<br />";
}
好的答案,我有另一個快速的給你。如果我想獲得一個特定的stats-> name(例如「tk」)並返回它的值(例如「57656」),那麼我可以爲每一個創建getFunctions我將如何執行此操作? – KHAN
@KHAN檢查編輯。 – Kamehameha