按JSON数据中的子值对数组进行排序

时间:2014-04-29 00:10:04

标签: php arrays json sorting

{
   "queue": "RANKED_SOLO_5x5",
   "name": "Taric's Enforcers",
   "entries": [
      {
         "leaguePoints": 502,
         "isFreshBlood": false,
         "isHotStreak": true,
         "rank": "I",
         "leagueName": "Taric's Enforcers",
         "isInactive": false,
         "queueType": "RANKED_SOLO_5x5",
         "isVeteran": true,
         "tier": "CHALLENGER",
         "lastPlayed": -1,
         "playerOrTeamName": "CaliforniaTrlolz",
         "playerOrTeamId": "21059607",
         "wins": 155
      },
      {
         "leaguePoints": 478,
         "isFreshBlood": false,
         "isHotStreak": false,
         "rank": "I",
         "leagueName": "Taric's Enforcers",
         "isInactive": false,
         "queueType": "RANKED_SOLO_5x5",
         "isVeteran": false,
         "tier": "CHALLENGER",
         "lastPlayed": -1,
         "playerOrTeamName": "Knives Millions",
         "playerOrTeamId": "21088719",
         "wins": 116
      },

我有这个长json数据,我需要它按“leaguePoints”键值从最高到最低排序。我已经使用json_decode设置为true将整个json放入一个支持PHP的关联数组中,但我无法通过值“leaguePoints”对其进行排序。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

这样的东西?

$array = json_decode($json, true);
usort($array['entries'], function($a, $b) {
    return $b['leaguePoints'] - $a['leaguePoints'];
});

在这里演示 - http://ideone.com/RTlxc3