2014-03-03 2 views
0

현재 Riot Games API를 사용 중이고 커뮤니티에서 개발 한 래퍼 중 하나 (https://github.com/kevinohashi/php-riot-api)를 사용 중입니다. 내 문제는 내가 arsort를 사용하여 결과를 정렬하기 위해 노력하고있다PHP/JSON 배열을 가장 높은 순으로 정렬 - Riot API

내 코드 예제 :

<?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]; 
} 

?> 

내 목표는 일종의 리그 포인트에 의해 배열 (최고 가장 낮은), 그러나 한 번 배열의 출력 당신이 볼 수 있듯이 나는 그것을 인쇄하지 않았습니다. 나는 아마 아주 작은 것을 놓치고있을 것이다. 그러나 어떤 도움이라도 크게 평가 될 것이다.

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 
) 
+0

무엇처럼'$ tempArray' 보입니까? –

+0

단지 다음 인쇄 : 어레이 ( [24381045] => 0 )을에 usort 함수 치료의 단점은 낮은 최고까지 배열에 나열된 리그 점의 개수 일 –

답변

1

$array은 하나의 배열을 가진 배열입니다. 위에서 여러 리그를 지원하지 않습니다

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

참고 :

은 아마도 당신은 당신이 당신의 코드를 조정할 수있는 경우에는 항목의 배열을 정렬 할 수 있습니다.

는 좀 더 읽을 수 usort를 사용하여 찾을 그러나 :

foreach($array as $key => $league){ 
    usort($array[$key]['entries'], function($a,$b){ 
     return $a['leaguePoints'] - $b['leaguePoints']; 
    }); 
} 
+0

- 어떤 것을 가장 높은 것부터 가장 낮은 것까지의 이상적인 정렬 방법? –

+0

@DanWhiteside'ursort'로 전환하거나 compare 함수를'return $ b [ 'leaguePoints'] - $ a [ 'leaguePoints'];'로 변경할 수 있습니다. – Jim

+0

그냥 비교 기능을 변경하고 당신이 제안한 것을하려고했습니다! 매력을 발휘합니다 - 내가 계획 한 나머지 코드는 평범한 항해입니다. 많은 분들께서 Jim이 다시 한 번 감사드립니다. –

관련 문제