Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently playing with Riot Games API and using one of the wrappers developed by the community (https://github.com/kevinohashi/php-riot-api). My issue is, I am trying to sort the results using arsort

My code example:

<?php
include('php-riot-api.php');

$summoner_name = 'fallingmoon';
$summoner_id = 24381045;

$test = new riotapi('euw');

$r = $test->getLeague($summoner_id);

?>

<?php

$array = json_decode($r, true); 

foreach($array AS $key => $newArray) {
$tempArray[$key] = $newArray['entries'][0]['leaguePoints'];
}

arsort($tempArray);
$finalArray = array();

foreach($tempArray AS $key => $value) {
$finalArray[] = $array[$key];
}

?>

My goal is sort the array by league points (Highest to lowest), but the output of the array once I print it is as followed, as you can see it hasn't sorted. I am probably missing something very minor but any help will be greatly appreciated.

Array
(
[0] => Array
(
[name] => Rengar's Demolishers
[tier] => GOLD
[queue] => RANKED_SOLO_5x5
[entries] => Array
(
[0] => Array
(
[playerOrTeamId] => 33372844
[playerOrTeamName] => L3tsPl4yLoL
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => V
[leaguePoints] => 0
[wins] => 34
[isHotStreak] => 1
[isVeteran] => 
[isFreshBlood] => 
[isInactive] => 
[lastPlayed] => -1
)

[1] => Array
(
[playerOrTeamId] => 19397582
[playerOrTeamName] => Lunchi
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => IV
[leaguePoints] => 10
[wins] => 7
[isHotStreak] => 
[isVeteran] => 
[isFreshBlood] => 
[isInactive] => 
[lastPlayed] => -1
)

[2] => Array
(
[playerOrTeamId] => 24613501
[playerOrTeamName] => RadiantBurst
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => II
[leaguePoints] => 42
[wins] => 48
[isHotStreak] => 
[isVeteran] => 
[isFreshBlood] => 
[isInactive] => 
[lastPlayed] => -1
)

[3] => Array
(
[playerOrTeamId] => 19939979
[playerOrTeamName] => vinter
[leagueName] => Rengar's Demolishers
[queueType] => RANKED_SOLO_5x5
[tier] => GOLD
[rank] => I
[leaguePoints] => 38
[wins] => 57
[isHotStreak] => 
[isVeteran] => 
[isFreshBlood] => 
[isInactive] => 
[lastPlayed] => -1
)
share|improve this question
    
What does the $tempArray look like? –  Nouphal.M Mar 3 at 16:45
    
it just prints the following: Array ( [24381045] => 0 ) –  Dan Whiteside Mar 3 at 16:58

1 Answer 1

up vote 0 down vote accepted

The issue is that $array is an array with one array in it.

Presumably you want to sort the entries array in which case you can tweak your code:

foreach($array[0]['entries'] AS $key => $team) {
    $tempArray[$key] = $team['leaguePoints'];
}

arsort($tempArray);
$finalArray = array();

foreach($tempArray AS $key => $value) {
    $finalArray[] = $array[0]['entries'][$key];
}

Note that the above doesn't support multiple leagues.

However I find using usort to be more readable:

foreach($array as $key => $league){
    usort($array[$key]['entries'], function($a,$b){
        return $a['leaguePoints'] - $b['leaguePoints'];
    });
}
share|improve this answer
    
the usort function worked a treat, the downside is the number of league points is listed in the array from lowest to highest - what would be the ideal sorting method for highest to lowest? –  Dan Whiteside Mar 4 at 13:48
    
@DanWhiteside You can either switch to ursort or change the compare function to return $b['leaguePoints'] - $a['leaguePoints'];. –  Jim Mar 4 at 14:02
    
Just changed the compare function and was about to do what you suggested! Works a charm - the rest of the code I have planned is plain sailing, Many thanks Jim once again. –  Dan Whiteside Mar 4 at 14:09
    
@DanWhiteside No problem, glad I could help. –  Jim Mar 4 at 14:27
    
I am using the tweaked code above with a similar array (example of output link from the same API - one of the errors is Cannot use a scalar value as an array and the other is usort() expects parameter 1 to be array, null given –  Dan Whiteside Mar 5 at 12:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.