0

json url

i wanna sort champions array. I want to sort it by " champions -> 0 -> stats -> totalSessionsPlayed " But i can not. How can i short array ?

$url = 'https://tr.api.pvp.net/api/lol/tr/v1.3/stats/by-summoner/3800684/ranked?season=SEASON2016&api_key=RGAPI-2F65B634-F9C5-4DA7-A5E3-1D955D5D1E3B';
$content = file_get_contents($url);
$arr = json_decode($content);
$sorted = sort(array_column($arr, 'totalSessionsPlayed')); 

ı found this code but ıt doesn't work.

4
  • 1
    Please include the relevant JSON in your question body. Commented Oct 10, 2016 at 13:55
  • 2
    Please not all of it, though. Commented Oct 10, 2016 at 13:57
  • Only relevant JSON, please. Commented Oct 10, 2016 at 13:59
  • @kyhn1239. You need to get the data from totalSessionsPlayed alone in an array right. have i understood the question well. or it is outputed some other way Commented Oct 10, 2016 at 14:21

2 Answers 2

1

Use usort with custom comparison function:

$champions = $arr->champions;
// usort alters the input array, no need to assign
usort($champions , function($a, $b) {
    // Will sort in descending order, for ascending, switch sides
    return $b->stats->totalSessionsPlayed - $a->stats->totalSessionsPlayed;
});

Basically, you have to sort the first level of champions array and compare lower values internally.

Your code does not work, because there is no totalSessionsPlayed key in $arr array directly.

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

Comments

0

I hope this can help you.

Here I am using the nested foreach loop for the first Object and then second for the second Object . Using the array_filter helps to remove the empty string/element. as it returns false when an empty element is present. SORT function Returns TRUE on success or FALSE on failure. So that's why we used the last loop to display the sorted result.

<?php 
$url = 'https://tr.api.pvp.net/api/lol/tr/v1.3/stats/by-summoner/3800684/ranked?season=SEASON2016&api_key=RGAPI-2F65B634-F9C5-4DA7-A5E3-1D955D5D1E3B';
$content = file_get_contents($url);
$arr = json_decode($content);
$champions = ($arr->champions); 
foreach($champions as $champion){
    $champion_totalSessionsPlayed = $champion;

    foreach($champion_totalSessionsPlayed as $champions_totalSessionsPlayed){
        $totalSessionsPlayed = $champions_totalSessionsPlayed->totalSessionsPlayed;
            $totalSessionsPlayed_array[] = $totalSessionsPlayed;
    }
}
$totalSessionsPlayed = array_filter($totalSessionsPlayed_array);
sort($totalSessionsPlayed);
$arrlength = count($totalSessionsPlayed);
for($x = 0; $x < $arrlength; $x++) {
    echo $totalSessionsPlayed[$x];
    echo "<br>";
}
?>

5 Comments

Because your answer lacks an explanation. Code-only answers are strongly discouraged.
OfCourse I will but what kind of explaination does this code require? it is just a simple foreach loop and then extracting the values and then it sorts the values?
Explain why you are using nested for loops. Explain what array_filter does. Explain why you are calling sort. Explain what the final for loop does. Ofcourse, seasoned developers can see what it does, but that is not the point. See this meta discussion
@ Pieter van den Ham : Answer Edited. Is that OK now?
Imagine you're talking to a beginner. Explain clearly what they need to do. A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.

Your Answer

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