如何使用foreach调用api的每个部分?

时间:2015-09-29 16:55:18

标签: php json api

我有一个问题,我想我会在单独尝试之后问这里。我一直在努力从我必须使用的api调用几个数组。我正在尝试使用foreach,但当前代码只显示来自api的最后一个对象。

PHP:

$json_array = json_decode(file_get_contents('https://api.ark.bar/v1/server/209.170.149.98/16753'), true);
$name = $json_array['server']['name'];
$version = $json_array['server']['version'];
$map = $json_array['server']['map'];
$time = $json_array['server']['time'];
$playercount = $json_array['server']['playerCount'];
$maxplayers = $json_array['server']['playerMax'];
$secure =  $json_array['server']['secure'];

foreach($json_array['server']['players'] as $info) {
    $playerName = $info['name'];
    $playerScore = $info['score'];
    $playerTime = $info['time'];
    $playerHumanTime = $info['humanTime'];
}

if($json_array['secure'] = true) {
    $secureTrue = "Yes";
} else {
    $secureTrue = "No";
}
if($json_array['status'] != NULL) {
    echo "<h2>Name: $name</h2><p>Version: $version</p><p>Map: $map</p><p>Time: $time</p><p>Player Count: $playercount</p><p>Max Players: $maxplayers</p><p>Secure: $secureTrue</p>";
    echo "<h2>Players</h2><p>$playerName</p><p>$playerScore</p><p>$playerTime</p><p>$playerHumanTime</p>";
} else {
    echo "<p>Server is Offline</p>";    
}

JSON:

{
    "status": true,
    "server": {
        "name": "Server Name",
        "version": 216.2,
        "map": "TheIsland",
        "time": "07:12",
        "port": 16743,
        "game": "ARK: Survival Evolved",
        "environment": "Windows",
        "secure": true,
        "playerCount": 3,
        "playerMax": 60,
        "players": [
            {
                "name": "=[A51]=Sarge",
                "score": 0,
                "time": 20520.654296875,
                "humanTime": "5 hours, 42 minutes"
            },
            {
                "name": "Tureman",
                "score": 0,
                "time": 15358.5849609375,
                "humanTime": "4 hours, 16 minutes"
            },
            {
                "name": "Borgscan",
                "score": 0,
                "time": 12156.5673828125,
                "humanTime": "3 hours, 23 minutes"
            }
        ]
    }
}

最终结果是信息传递到页面但玩家信息被完全忽略,除了最后一个玩家。我已经搜索了如何在foreach中标记它们但我无法得到最终结果。 :(

5 个答案:

答案 0 :(得分:1)

foreach 的每次迭代都会覆盖最后一次。

只要$info获得新的数组实例化,接收其字段的每个变量都会重新分配新的字段值。

由于您的输出位于循环之外,它只会写出分配给变量的最后一个值。

您必须将echo放入foreach内,而不是将其分配给新的广告。

遵循您的代码逻辑,它将类似于:

if($json_array['status'] != NULL)
{
    echo "<h2>Name: $name</h2><p>Version: $version</p><p>Map: $map</p><p>Time: $time</p><p>Player Count: $playercount</p><p>Max Players: $maxplayers</p><p>Secure: $secureTrue</p>";
    foreach($json_array['server']['players'] as $info)
        echo "<h2>Players</h2><p>$info['name'];</p><p>$info['score'];</p><p>$info['time'];</p><p>$info['humanTime'];</p>";
}

并注意您的代码缩进,以提高其可读性。

答案 1 :(得分:0)

您每次循环并重新分配变量,因此只有最后一个变量将分配给您拥有的变量。尝试

foreach($json_array['server']['players'] as $info)
                {
                $playerName = $info['name'];
                $playerScore = $info['score'];
                $playerTime = $info['time'];
                $playerHumanTime = $info['humanTime'];

                if($json_array['secure'] = true) {
                   $secureTrue = "Yes";
                } else {
                   $secureTrue = "No";
                }
                if($json_array['status'] != NULL) {
                   echo "<h2>Name: $name</h2><p>Version: $version</p><p>Map: $map</p><p>Time: $time</p><p>Player Count: $playercount</p><p>Max Players: $maxplayers</p><p>Secure: $secureTrue</p>";
                   echo "<h2>Players</h2><p>$playerName</p><p>$playerScore</p><p>$playerTime</p><p>$playerHumanTime</p>";
                } else {
                  echo "<p>Server is Offline</p>";    
                }
                }

答案 2 :(得分:0)

您的代码正在重写变量,然后在循环完成后打印它。将输出代码移动到循环中。就个人而言,我主张将一个文本字符串分配给一个变量,然后在循环之后输出它(它更快)。

$json_array = json_decode(file_get_contents('https://api.ark.bar/v1/server/209.170.149.98/16753'), true);
$name = $json_array['server']['name'];
$version = $json_array['server']['version'];
$map = $json_array['server']['map'];
$time = $json_array['server']['time'];
$playercount = $json_array['server']['playerCount'];
$maxplayers = $json_array['server']['playerMax'];
$secure =  $json_array['server']['secure'];
$output = "<h2>Name: $name</h2><p>Version: $version</p><p>Map: $map</p><p>Time: $time</p><p>Player Count: $playercount</p><p>Max Players: $maxplayers</p><p>Secure: $secureTrue</p>";

foreach($json_array['server']['players'] as $info) {
    $playerName = $info['name'];
    $playerScore = $info['score'];
    $playerTime = $info['time'];
    $playerHumanTime = $info['humanTime'];

    $output.= "<h2>Players</h2><p>$playerName</p><p>$playerScore</p><p>$playerTime</p><p>$playerHumanTime</p>";
}

if($json_array['secure'] = true) {
    $secureTrue = "Yes";
    echo $output;
} else {
    $secureTrue = "No";
}
if($json_array['status'] != NULL) {
    echo $output;
} else {
    echo "<p>Server is Offline</p>";    
}

答案 3 :(得分:0)

您可能希望将players分配给array,然后将其循环输出,否则,您将只获得最后一位玩家,即:

<?php

$json_array = json_decode(file_get_contents('https://api.ark.bar/v1/server/209.170.149.98/16753'), true);
$name = $json_array['server']['name'];
$version = $json_array['server']['version'];
$map = $json_array['server']['map'];
$time = $json_array['server']['time'];
$playercount = $json_array['server']['playerCount'];
$maxplayers = $json_array['server']['playerMax'];
$secure =  $json_array['server']['secure'];

foreach($json_array['server']['players'] as $info)
{
    $playerName[] .= $info['name'];
    $playerScore[] .= $info['score'];
    $playerTime[] .= $info['time'];
    $playerHumanTime[] .= $info['humanTime'];
}

if($json_array['secure'] = true) {
    $secureTrue = "Yes";
} else {
    $secureTrue = "No";
}
if($json_array['status'] != NULL) {
     echo "<h2>Name: $name</h2><p>Version: $version</p><p>Map: $map</p><p>Time: $time</p><p>Player Count: $playercount</p><p>Max Players: $maxplayers</p><p>Secure: $secureTrue</p>";
     for($i=1; $i < count($playerName); $i++){
         echo "<h2>Players</h2><p>{$playerName[$i]}</p><p>{$playerScore[$i]}</p><p>{$playerTime[$i]}</p><p>{$playerHumanTime[$i]}</p>";
     }

 } else {
     echo "<p>Server is Offline</p>";    
 }
?>

我已经测试了代码,它按预期工作。

答案 4 :(得分:0)

像这样查找你的代码

=MONTHNAME(IIF(MONTH(Today)+4>12,MONTH(Today)+4-12,MONTH(Today)+4), TRUE) & " " &
IIF(MONTH(Today)+4>12,YEAR(NOW)+1,YEAR(NOW))