[英]PHP/JSON array sorting by highest number - Riot API
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 ). 我目前正在使用Riot Games API,并使用社区开发的包装器之一( https://github.com/kevinohashi/php-riot-api )。 My issue is, I am trying to sort the results using arsort
我的问题是,我正在尝试使用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
)
The issue is that $array
is an array with one array in it. 问题是
$array
是一个其中包含一个数组的数组。
Presumably you want to sort the entries array in which case you can tweak your code: 大概您想对entrys数组进行排序,在这种情况下,您可以调整代码:
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: 但是我发现使用
usort
更具可读性:
foreach($array as $key => $league){
usort($array[$key]['entries'], function($a,$b){
return $a['leaguePoints'] - $b['leaguePoints'];
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.