如何获得没有密钥的json值

时间:2015-04-05 07:34:06

标签: php json steam

我使用php来获取json值,并且通过数据循环并且回显SteamName,Level,Experience等没有问题。但是,我不知道如何回应每个流行的蒸汽号码7656119801319。

json文件:

{
  "FURNACES": {},
  "PROFILE": {
    "76561198013191579": {
      "SteamName": "Cayos",
      "Level": 22,
      "Experience": 1592,
      "Agility": 22,
      "Strength": 22,
      "Intelligence": 22,
      "StatsPoints": 66,
      "SkillPoints": 2,
      "Skills": {
        "lumberjack": 20
      },
      "Preferences": {
        "ShowXPMessagePercent": 0.25,
        "ShowCraftMessage": true,
        "UseBlinkArrow": true,
        "AutoToggleBlinkArrow": true
      }
    },
    "76561198113765447": {
      "SteamName": "CaptainCheesy",
      "Level": 31,
      "Experience": 3576,
      "Agility": 31,
      "Strength": 70,
      "Intelligence": 70,
      "StatsPoints": 15,
      "SkillPoints": 5,
      "Skills": {
        "miner": 8,
        "lumberjack": 10,
        "hunter": 8
      },
      "Preferences": {
        "ShowXPMessagePercent": 0.25,
        "ShowCraftMessage": true,
        "UseBlinkArrow": true,
        "AutoToggleBlinkArrow": true
      }
    },
    "76561198141845337": {
      "SteamName": "Carrot",
      "Level": 3,
      "Experience": 409,
      "Agility": 3,
      "Strength": 3,
      "Intelligence": 3,
      "StatsPoints": 9,
      "SkillPoints": 3,
      "Skills": {},
      "Preferences": {
        "ShowXPMessagePercent": 0.25,
        "ShowCraftMessage": true,
        "UseBlinkArrow": true,
        "AutoToggleBlinkArrow": true
      }
    },
    "76561198012539649": {
      "SteamName": "Booglee",
      "Level": 3,
      "Experience": 110,
      "Agility": 3,
      "Strength": 3,
      "Intelligence": 3,
      "StatsPoints": 9,
      "SkillPoints": 3,
      "Skills": {},
      "Preferences": {
        "ShowXPMessagePercent": 0.25,
        "ShowCraftMessage": true,
        "UseBlinkArrow": true,
        "AutoToggleBlinkArrow": true
      }
    },
    "333228975": {
      "SteamName": "SYN Gaurd",
      "Level": 0,
      "Experience": 0,
      "Agility": 0,
      "Strength": 0,
      "Intelligence": 0,
      "StatsPoints": 0,
      "SkillPoints": 0,
      "Skills": {},
      "Preferences": {
        "ShowXPMessagePercent": 0.25,
        "ShowCraftMessage": true,
        "UseBlinkArrow": true,
        "AutoToggleBlinkArrow": true
      }
    }
  }
}

我的PHP代码:

$url = 'C:\Users\plosey\Desktop\GameServers\Rust\steam\server_syn1\server\syn1\oxide\data\Hunt_Data.json';  //Url to the cars api
$content = file_get_contents($url);
$json = json_decode($content);
    foreach($json->PROFILE as $user) {
          echo $user->SteamName;  
          echo $user->NEED STEAM ID HERE;  // This is the line that is not correct.
} 

所以简而言之,我不仅需要回显SteamName,还要回复来自相同个人资料的Steamid号码。

3 个答案:

答案 0 :(得分:3)

而不是

foreach($json->PROFILE as $user) {

使用

foreach ($json->PROFILE as $steamid => $user) {

答案 1 :(得分:2)

尝试下面的代码来打印ID。 $ stremId将打印您的ID。您可以用任何变量名替换它。

$json = json_decode($json);
foreach($json->PROFILE as $streamId => $user) {
          echo $user->SteamName;
          echo $streamId;
} 

答案 2 :(得分:0)

试试这个

foreach($json->PROFILE as $key => $user) {
        echo $key .' => '.$user->SteamName."<br/>";
}