0

I have been curious about PHP lately and I am working on a test subject. I want to get the number of citizens from an online game and order it by Military rank.

Here is the link of the API: https://www.erevollution.com/en/api/citizenship/1

Here is the code I have so far.

<form action="index.php" method="post">
    <input type="text" name="id"><br>
    <input type="submit">
</form>
<?php   
$okey= $_POST["id"];;
$jsonurl="https://www.erevollution.com/en/api/citizenship/".$okey;
$json = file_get_contents($jsonurl,0,null,null);  
$json_output = json_decode($json); 
echo "Players of albania are: <br>";  

foreach ($json_output as $trend)  
{   
    $id = $trend->ID;
    echo " Name : {$trend->Name}\n";    
    echo '<br>';
}  
4
  • 2
    you have given the url and the code what is the problem ? Commented Aug 11, 2016 at 15:19
  • @StackB00m i would like to order the citizens from military rank Commented Aug 11, 2016 at 15:20
  • @StackB00m can you give me a code snippet ? i am kinda noob ! Commented Aug 11, 2016 at 15:25
  • hey I cant now code a snippet for you but i can give you the best reference to the subject : webtutsdepot.com/2009/08/31/how-to-read-json-data-with-php Commented Aug 11, 2016 at 15:29

4 Answers 4

1

When you json_decode the API response, use true for the second parameter to get an associative array rather than a stdClass object.

$json_output = json_decode($json, true); 

Then you can use usort to sort by MilitaryRank:

usort($json_output, function($a, $b) {
    if ($a['MilitaryRank'] < $b['MilitaryRank']) return -1;
    if ($a['MilitaryRank'] > $b['MilitaryRank']) return 1;
    return 0;
});

If you want to sort descending rather than ascending, just reverse the two if conditions.

Sign up to request clarification or add additional context in comments.

Comments

0

There's an example on the usort docs for sorting a multidimensional array. Basically just substitute your desired array index 'MilitaryRank'

I also zazzed up the HTML a little more to make it more readable.

<form method="post">
    <input type="text" name="id"><br>
    <input type="submit">
</form>
<?php   
$okey= $_POST["id"];;
$jsonurl="https://www.erevollution.com/en/api/citizenship/".$okey;
$json = file_get_contents($jsonurl,0,null,null);  
$json_output = json_decode($json, true); 

// print_r($json_output);

function cmp($a, $b)
{
    if ($a['MilitaryRank'] == $b['MilitaryRank']) {
        return 0;
    }
    return ($a['MilitaryRank'] < $b['MilitaryRank']) ? -1 : 1;
}

usort($json_output, "cmp");

echo "<h1>Players of albania are: </h1>";  

foreach ($json_output as $trend)  
{
    $id = $trend['ID'];
    echo " Name : $trend[Name]\n<br>";
    echo " MRank : $trend[MilitaryRank]\n<br><hr/>";
}

1 Comment

Thank You ! This was the one i was looking for
0
$json_decoded = json_decode($json,true);

$allDatas = array();
foreach ($json_decoded as $user) {
    $allDatas[$user['MilitaryRank']][] = $user;
}
sort($allDatas);
print_r($allDatas);

so you can do a foreach like that :

foreach ($allDatas as $MilitaryRank => $users) {
    # code...
}

Comments

0

Here's my solution if I got it right, otherwise, correct me!

<?php
$jsonurl="https://www.erevollution.com/en/api/citizenship/1";
$json = file_get_contents($jsonurl,0,null,null);  
$json_output = json_decode($json, true);
echo '<pre>';
echo "Players of albania are: <br>";
$military_rank = [];  
foreach ($json_output as $trend)  
{   
    $military_rank[$trend['MilitaryRank']][] = $trend;
}
ksort($military_rank);
foreach ($military_rank as $key => $rank)
{
    echo '<br><br>Rank ' . $key . '<br>';
    foreach ($rank as $player)
    {
        echo 'Name: ' . $player['Name'] . '<br>';
    }
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.