在 PHP 中从 JSON 获取单个项目

我已经搜索了堆栈溢出,但没有找到我想要做什么...所以这里的问题是: 如何仅返回 STEAMID?


代码:


<?php

// define variables and set to empty values

$steamID = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

  $steamID = test_input($_POST["steamID"]);

}

function test_input($data) {

  $data = trim($data);

  $data = stripslashes($data);

  $data = htmlspecialchars($data);

  return $data;

}

$keyApp = '5DE00EE4D07BA728EDAB0B3C2961AA6B';

$api = "";

$json_data = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$keyApp&steamids=$steamID");

$array = json_decode($json_data, true);

var_dump($array);

?>

var_转储:


array(1) {

  ["response"]=>

  array(1) {

    ["players"]=>

    array(1) {

      [0]=>

      array(19) {

        ["steamid"]=>

        string(17) "76561198058543756"

        ["communityvisibilitystate"]=>

        int(3)

        ["profilestate"]=>

        int(1)

        ["personaname"]=>

        string(12) ""Cientistas""

        ["commentpermission"]=>

        int(1)

        ["profileurl"]=>

        string(37) "https://steamcommunity.com/id/rhatto/"

        ["avatar"]=>

        string(116) "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c9/c9442f1ea597a7685f8e50f2f7c922321beb4eba.jpg"

        ["avatarmedium"]=>

        string(123) "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c9/c9442f1ea597a7685f8e50f2f7c922321beb4eba_medium.jpg"

        ["avatarfull"]=>

        string(121) "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c9/c9442f1ea597a7685f8e50f2f7c922321beb4eba_full.jpg"

        ["avatarhash"]=>

        string(40) "c9442f1ea597a7685f8e50f2f7c922321beb4eba"

        ["lastlogoff"]=>

        int(1593317334)

        ["personastate"]=>

        int(0)


蝴蝶不菲
浏览 146回答 2
2回答

Helenr

通过访问唯一的正确路径steamid,显然玩家数组中可能有多个项目。所以你可能需要某种循环。也许是这样的:$players = $array['response']['players'];foreach($players as $player) {&nbsp; echo $player['steamid'] . '<br>';}或者,如果你的玩家数组中总是有 1 个玩家,你也可以这样做(快速而肮脏):echo $array['response']['players'][0]['steamid'];

BIG阳

$steamID = $array['response']['players'][0]['steamid'];
打开App,查看更多内容
随时随地看视频慕课网APP