Martin Spatovaliyski
Martin Spatovaliyski

Reputation: 15

How do I call every piece of the api using foreach?

I have an issue and I thought I'd ask here after trying alone.I've been struggling to call several arrays from the api that I have to use. I'm trying with foreach but the current code displays only the last object from the 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"
            }
        ]
    }
}

The end result is the information getting delivered to the page but the player info gets completely ignored except the last player. I've searched around how to flag them in the foreach but I can't get end results. :(

Upvotes: 1

Views: 130

Answers (5)

user5330699
user5330699

Reputation:

Chage your code like this

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>";
    $playerNum=1;
    foreach($json_array['server']['players'] as $info) {
        $playerName = $info['name'];
        $playerScore = $info['score'];
        $playerTime = $info['time'];
        $playerHumanTime = $info['humanTime'];
        echo "<h3>Player $playerNum</h3><p>$playerName</p><p>$playerScore</p><p>$playerTime</p><p>$playerHumanTime</p>";
        $playerNum++;
}

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98881

You may want to assign players to an array, and loop it for the output, otherwise, you'll only get the last player, i.e:

<?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>";    
 }
?>

I've tested the code and it works as intended.

Upvotes: 0

al&#39;ein
al&#39;ein

Reputation: 1729

Each iteration of your foreach overwrites the last one.

As soon as $info gets a new array instancing, every variable that receives its fields are reassigned new fields values.

As your output lies outside your loop, it'll only write out the last value assigned to your variables.

You have to put your echos inside your foreach instead of assigning them to new vars.

Following your code's logic, it would be something like:

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>";
}

And beware your code indentation to improve it's readability.

Upvotes: 1

smcjones
smcjones

Reputation: 5600

Your code is rewriting a variable and then printing it after the loop is completed. Move your output code into the loop. Personally, I advocate assigning a text string to a variable and then outputting it after the loop (it's faster).

$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>";    
}

Upvotes: 0

Cameron Spanos
Cameron Spanos

Reputation: 250

You are looping through and reassigning the variable each time so only the last one will be assigned to the variables you have. try

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>";    
                }
                }

Upvotes: 0

Related Questions