Nikoljan Behari
Nikoljan Behari

Reputation: 11

Ordering json api with php

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

Upvotes: 0

Views: 138

Answers (4)

Jeff Puckett
Jeff Puckett

Reputation: 40971

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

Upvotes: 0

rosengrenen
rosengrenen

Reputation: 771

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

Upvotes: 0

MaximeK
MaximeK

Reputation: 2071

$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...
}

Upvotes: 0

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

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.

Upvotes: 1

Related Questions