簡體   English   中英

使用PHP從json抓取未命名(?)數據

[英]Grabbing unnamed(?) data from json with PHP

我在這里有點麻煩...

我正在嘗試從此處的json文件中獲取一些值並且可以在此處將其格式化。

json文件如下所示:

{
"type": "success",
"message": "OK",
"data": {
"mainWeaponStats": [
{
"category": "Machine guns",
"timeEquipped": 3507,
"startedWith": null,
"code": "mgRPK",
"headshots": 18,
"name": "RPK",
"kills": 100,
"deaths": null,
},
{
"category": "Handheld weapons",
"timeEquipped": 5452,
"startedWith": null,
"code": "wahUGL",
"headshots": 1,
"name": "Underslung Launcher",
"kills": 108,
"deaths": null,
},
{
"category": "Sniper rifles",
"timeEquipped": 307,
"startedWith": null,
"code": "srMK11",
"headshots": 0,
"name": "MK11",
"kills": 2,
"deaths": null,
},

等等。

我想抓住其中一項的殺戮。 意思是我想給一個像“ Underslung Launcher”的參數並返回108。在這種情況下,就是“ Underslung Launcher”。 我正在尋找這樣的代碼:

$gamemode = $decode['data']['topStats']['mapMode'];

但是,如果有人知道更好的方法,請告訴我。 由於列表中的項目沒有“名稱”,因此與“數據”和“ mainWeaponStats”不同,我無法真正弄清楚該如何做。

編輯:這是到目前為止的相關代碼:

$weaponstats = "http://battlelog.battlefield.com/bf3/weaponsPopulateStats/" . $bf3id . "/1/";
$content = file_get_contents($weaponstats);
$decode = json_decode($content, true);

$mainweaponstats = $decode['data']['mainWeaponStats'];

如您所見,我在學習Json方面遇到了困難。 我正在嘗試閱讀它,但是到目前為止,我還不能弄清楚。

我真的不知道該怎么做,因為我要查找的值在同一組中。

$mainweaponstats = $decode['data']['mainWeaponStats'];

這是一個對象數組(數組,因為您將,true傳遞給json_decode )。 只需循環瀏覽並找到您想要的。

$search = 'Underslung Launcher';
$kills = 0;
foreach($mainweaponstats as $w){
    if($w['name'] === $search){
        $kills = $w['kills'];
        break;
    }
}
echo $kills;
<?

$name = "Underslung Launcher";

$json = file_get_contents("json.php");
$dec = array();
$dec = json_decode($json,true);

$datas = $dec['data']['mainWeaponStats'];

foreach($datas as $data)
{
    if($data['name']==$name) {
        echo $data['kills'];
        break;
    }
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM