我想在PHP 5.4中按子数组的值对数组条目进行排序,但我不确定如何。
假设我想通过$ result ['champions'] ['stats'] ['totalSessionsPlayed']对数组进行数值排序。
我用Google搜索了一下,找不到任何针对此的内容,但我可能会搜索错误的内容。
非常感谢任何帮助,提前谢谢!
示例数组:
{
"modifyDate": 1393185310000,
"champions": [
{
"id": 75,
"stats": {
"totalDeathsPerSession": 58,
"totalSessionsPlayed": 15,
"totalDamageTaken": 493121,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 2396,
"maxChampionsKilled": 6,
"totalDoubleKills": 4,
"totalPhysicalDamageDealt": 1585013,
"totalChampionKills": 51,
"totalAssists": 100,
"mostChampionKillsPerSession": 6,
"totalDamageDealt": 1930578,
"totalFirstBlood": 0,
"totalSessionsLost": 6,
"totalSessionsWon": 9,
"totalMagicDamageDealt": 325337,
"totalGoldEarned": 156883,
"totalPentaKills": 0,
"totalTurretsKilled": 18,
"mostSpellsCast": 0,
"maxNumDeaths": 7,
"totalUnrealKills": 0
},
"name": "Nasus"
},
{
"id": 5,
"stats": {
"totalDeathsPerSession": 6,
"totalSessionsPlayed": 1,
"totalDamageTaken": 30087,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 145,
"maxChampionsKilled": 4,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 58659,
"totalChampionKills": 4,
"totalAssists": 14,
"mostChampionKillsPerSession": 4,
"totalDamageDealt": 93944,
"totalFirstBlood": 0,
"totalSessionsLost": 0,
"totalSessionsWon": 1,
"totalMagicDamageDealt": 35285,
"totalGoldEarned": 11514,
"totalPentaKills": 0,
"totalTurretsKilled": 1,
"mostSpellsCast": 0,
"maxNumDeaths": 6,
"totalUnrealKills": 0
},
"name": "XinZhao"
},
{
"id": 76,
"stats": {
"totalDeathsPerSession": 8,
"totalSessionsPlayed": 2,
"totalDamageTaken": 27123,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 166,
"maxChampionsKilled": 5,
"totalDoubleKills": 1,
"totalPhysicalDamageDealt": 42820,
"totalChampionKills": 10,
"totalAssists": 14,
"mostChampionKillsPerSession": 5,
"totalDamageDealt": 148776,
"totalFirstBlood": 0,
"totalSessionsLost": 0,
"totalSessionsWon": 2,
"totalMagicDamageDealt": 105075,
"totalGoldEarned": 19656,
"totalPentaKills": 0,
"totalTurretsKilled": 3,
"mostSpellsCast": 0,
"maxNumDeaths": 4,
"totalUnrealKills": 0
},
"name": "Nidalee"
},
{
"id": 74,
"stats": {
"totalDeathsPerSession": 8,
"totalSessionsPlayed": 1,
"totalDamageTaken": 25708,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 313,
"maxChampionsKilled": 10,
"totalDoubleKills": 1,
"totalPhysicalDamageDealt": 23397,
"totalChampionKills": 10,
"totalAssists": 10,
"mostChampionKillsPerSession": 10,
"totalDamageDealt": 267380,
"totalFirstBlood": 0,
"totalSessionsLost": 0,
"totalSessionsWon": 1,
"totalMagicDamageDealt": 243833,
"totalGoldEarned": 19122,
"totalPentaKills": 0,
"totalTurretsKilled": 1,
"mostSpellsCast": 0,
"maxNumDeaths": 8,
"totalUnrealKills": 0
},
"name": "Heimerdinger"
},
{
"id": 36,
"stats": {
"totalDeathsPerSession": 22,
"totalSessionsPlayed": 4,
"totalDamageTaken": 85510,
"totalQuadraKills": 1,
"totalTripleKills": 1,
"totalMinionKills": 545,
"maxChampionsKilled": 9,
"totalDoubleKills": 1,
"totalPhysicalDamageDealt": 90623,
"totalChampionKills": 11,
"totalAssists": 28,
"mostChampionKillsPerSession": 9,
"totalDamageDealt": 346793,
"totalFirstBlood": 0,
"totalSessionsLost": 2,
"totalSessionsWon": 2,
"totalMagicDamageDealt": 254140,
"totalGoldEarned": 37894,
"totalPentaKills": 0,
"totalTurretsKilled": 2,
"mostSpellsCast": 0,
"maxNumDeaths": 9,
"totalUnrealKills": 0
},
"name": "DrMundo"
},
{
"id": 115,
"stats": {
"totalDeathsPerSession": 24,
"totalSessionsPlayed": 4,
"totalDamageTaken": 72093,
"totalQuadraKills": 0,
"totalTripleKills": 2,
"totalMinionKills": 684,
"maxChampionsKilled": 40,
"totalDoubleKills": 8,
"totalPhysicalDamageDealt": 70177,
"totalChampionKills": 48,
"totalAssists": 24,
"mostChampionKillsPerSession": 40,
"totalDamageDealt": 724195,
"totalFirstBlood": 0,
"totalSessionsLost": 3,
"totalSessionsWon": 1,
"totalMagicDamageDealt": 650827,
"totalGoldEarned": 51661,
"totalPentaKills": 0,
"totalTurretsKilled": 2,
"mostSpellsCast": 0,
"maxNumDeaths": 8,
"totalUnrealKills": 0
},
"name": "Ziggs"
},
{
"id": 10,
"stats": {
"totalDeathsPerSession": 7,
"totalSessionsPlayed": 1,
"totalDamageTaken": 15911,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 95,
"maxChampionsKilled": 2,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 15423,
"totalChampionKills": 2,
"totalAssists": 4,
"mostChampionKillsPerSession": 2,
"totalDamageDealt": 68032,
"totalFirstBlood": 0,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalMagicDamageDealt": 51891,
"totalGoldEarned": 6459,
"totalPentaKills": 0,
"totalTurretsKilled": 0,
"mostSpellsCast": 0,
"maxNumDeaths": 7,
"totalUnrealKills": 0
},
"name": "Kayle"
},
{
"id": 254,
"stats": {
"totalDeathsPerSession": 57,
"totalSessionsPlayed": 8,
"totalDamageTaken": 275656,
"totalQuadraKills": 0,
"totalTripleKills": 1,
"totalMinionKills": 569,
"maxChampionsKilled": 16,
"totalDoubleKills": 8,
"totalPhysicalDamageDealt": 1396894,
"totalChampionKills": 73,
"totalAssists": 79,
"mostChampionKillsPerSession": 16,
"totalDamageDealt": 1608228,
"totalFirstBlood": 0,
"totalSessionsLost": 5,
"totalSessionsWon": 3,
"totalMagicDamageDealt": 26289,
"totalGoldEarned": 114735,
"totalPentaKills": 0,
"totalTurretsKilled": 6,
"mostSpellsCast": 0,
"maxNumDeaths": 11,
"totalUnrealKills": 0
},
"name": "Vi"
},
{
"id": 84,
"stats": {
"totalDeathsPerSession": 12,
"totalSessionsPlayed": 1,
"totalDamageTaken": 40589,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 133,
"maxChampionsKilled": 9,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 77620,
"totalChampionKills": 9,
"totalAssists": 6,
"mostChampionKillsPerSession": 9,
"totalDamageDealt": 141249,
"totalFirstBlood": 0,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalMagicDamageDealt": 62145,
"totalGoldEarned": 12596,
"totalPentaKills": 0,
"totalTurretsKilled": 0,
"mostSpellsCast": 0,
"maxNumDeaths": 12,
"totalUnrealKills": 0
},
"name": "Akali"
},
{
"id": 81,
"stats": {
"totalDeathsPerSession": 48,
"totalSessionsPlayed": 11,
"totalDamageTaken": 240602,
"totalQuadraKills": 1,
"totalTripleKills": 1,
"totalMinionKills": 1896,
"maxChampionsKilled": 14,
"totalDoubleKills": 14,
"totalPhysicalDamageDealt": 1570961,
"totalChampionKills": 89,
"totalAssists": 75,
"mostChampionKillsPerSession": 14,
"totalDamageDealt": 1944153,
"totalFirstBlood": 0,
"totalSessionsLost": 6,
"totalSessionsWon": 5,
"totalMagicDamageDealt": 351296,
"totalGoldEarned": 147189,
"totalPentaKills": 0,
"totalTurretsKilled": 17,
"mostSpellsCast": 0,
"maxNumDeaths": 7,
"totalUnrealKills": 0
},
"name": "Ezreal"
},
{
"id": 17,
"stats": {
"totalDeathsPerSession": 4,
"totalSessionsPlayed": 1,
"totalDamageTaken": 15549,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 213,
"maxChampionsKilled": 5,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 55465,
"totalChampionKills": 5,
"totalAssists": 2,
"mostChampionKillsPerSession": 5,
"totalDamageDealt": 186354,
"totalFirstBlood": 0,
"totalSessionsLost": 0,
"totalSessionsWon": 1,
"totalMagicDamageDealt": 128342,
"totalGoldEarned": 13553,
"totalPentaKills": 0,
"totalTurretsKilled": 3,
"mostSpellsCast": 0,
"maxNumDeaths": 4,
"totalUnrealKills": 0
},
"name": "Teemo"
},
{
"id": 79,
"stats": {
"totalDeathsPerSession": 12,
"totalSessionsPlayed": 3,
"totalDamageTaken": 48189,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 432,
"maxChampionsKilled": 6,
"totalDoubleKills": 2,
"totalPhysicalDamageDealt": 40141,
"totalChampionKills": 11,
"totalAssists": 20,
"mostChampionKillsPerSession": 6,
"totalDamageDealt": 344112,
"totalFirstBlood": 0,
"totalSessionsLost": 1,
"totalSessionsWon": 2,
"totalMagicDamageDealt": 301544,
"totalGoldEarned": 28523,
"totalPentaKills": 0,
"totalTurretsKilled": 2,
"mostSpellsCast": 0,
"maxNumDeaths": 6,
"totalUnrealKills": 0
},
"name": "Gragas"
},
{
"id": 126,
"stats": {
"totalDeathsPerSession": 5,
"totalSessionsPlayed": 1,
"totalDamageTaken": 22882,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 161,
"maxChampionsKilled": 6,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 124060,
"totalChampionKills": 6,
"totalAssists": 7,
"mostChampionKillsPerSession": 6,
"totalDamageDealt": 134709,
"totalFirstBlood": 0,
"totalSessionsLost": 0,
"totalSessionsWon": 1,
"totalMagicDamageDealt": 10648,
"totalGoldEarned": 12014,
"totalPentaKills": 0,
"totalTurretsKilled": 0,
"mostSpellsCast": 0,
"maxNumDeaths": 5,
"totalUnrealKills": 0
},
"name": "Jayce"
},
{
"id": 56,
"stats": {
"totalDeathsPerSession": 13,
"totalSessionsPlayed": 1,
"totalDamageTaken": 45039,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 86,
"maxChampionsKilled": 8,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 194352,
"totalChampionKills": 8,
"totalAssists": 4,
"mostChampionKillsPerSession": 8,
"totalDamageDealt": 230377,
"totalFirstBlood": 0,
"totalSessionsLost": 1,
"totalSessionsWon": 0,
"totalMagicDamageDealt": 11050,
"totalGoldEarned": 14589,
"totalPentaKills": 0,
"totalTurretsKilled": 1,
"mostSpellsCast": 0,
"maxNumDeaths": 13,
"totalUnrealKills": 0
},
"name": "Nocturne"
},
{
"id": 35,
"stats": {
"totalDeathsPerSession": 12,
"totalSessionsPlayed": 2,
"totalDamageTaken": 58956,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 173,
"maxChampionsKilled": 10,
"totalDoubleKills": 1,
"totalPhysicalDamageDealt": 274083,
"totalChampionKills": 18,
"totalAssists": 20,
"mostChampionKillsPerSession": 10,
"totalDamageDealt": 447168,
"totalFirstBlood": 0,
"totalSessionsLost": 1,
"totalSessionsWon": 1,
"totalMagicDamageDealt": 125728,
"totalGoldEarned": 30451,
"totalPentaKills": 0,
"totalTurretsKilled": 4,
"mostSpellsCast": 0,
"maxNumDeaths": 6,
"totalUnrealKills": 0
},
"name": "Shaco"
},
{
"id": 60,
"stats": {
"totalDeathsPerSession": 36,
"totalSessionsPlayed": 7,
"totalDamageTaken": 159069,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 290,
"maxChampionsKilled": 14,
"totalDoubleKills": 3,
"totalPhysicalDamageDealt": 339038,
"totalChampionKills": 48,
"totalAssists": 40,
"mostChampionKillsPerSession": 14,
"totalDamageDealt": 982180,
"totalFirstBlood": 0,
"totalSessionsLost": 4,
"totalSessionsWon": 3,
"totalMagicDamageDealt": 555025,
"totalGoldEarned": 74004,
"totalPentaKills": 0,
"totalTurretsKilled": 2,
"mostSpellsCast": 0,
"maxNumDeaths": 8,
"totalUnrealKills": 0
},
"name": "Elise"
},
{
"id": 1,
"stats": {
"totalDeathsPerSession": 12,
"totalSessionsPlayed": 3,
"totalDamageTaken": 35249,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 108,
"maxChampionsKilled": 4,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 26391,
"totalChampionKills": 6,
"totalAssists": 26,
"mostChampionKillsPerSession": 4,
"totalDamageDealt": 114034,
"totalFirstBlood": 0,
"totalSessionsLost": 1,
"totalSessionsWon": 2,
"totalMagicDamageDealt": 87641,
"totalGoldEarned": 25827,
"totalPentaKills": 0,
"totalTurretsKilled": 3,
"mostSpellsCast": 0,
"maxNumDeaths": 6,
"totalUnrealKills": 0
},
"name": "Annie"
},
{
"id": 62,
"stats": {
"totalDeathsPerSession": 184,
"totalSessionsPlayed": 35,
"totalDamageTaken": 942852,
"totalQuadraKills": 0,
"totalTripleKills": 5,
"totalMinionKills": 2390,
"maxChampionsKilled": 19,
"totalDoubleKills": 41,
"totalPhysicalDamageDealt": 6088845,
"totalChampionKills": 294,
"totalAssists": 259,
"mostChampionKillsPerSession": 19,
"totalDamageDealt": 7057221,
"totalFirstBlood": 0,
"totalSessionsLost": 13,
"totalSessionsWon": 22,
"totalMagicDamageDealt": 242937,
"totalGoldEarned": 467825,
"totalPentaKills": 0,
"totalTurretsKilled": 27,
"mostSpellsCast": 0,
"maxNumDeaths": 12,
"totalUnrealKills": 0
},
"name": "MonkeyKing"
},
{
"id": 0,
"stats": {
"totalDamageTaken": 3960844,
"totalTripleKills": 17,
"totalMinionKills": 14847,
"maxChampionsKilled": 40,
"maxLargestCriticalStrike": 1304,
"totalChampionKills": 980,
"totalPhysicalDamageDealt": 17450877,
"rankedPremadeGamesPlayed": 0,
"totalSessionsLost": 67,
"totalNeutralMinionsKilled": 9357,
"totalSessionsWon": 77,
"totalMagicDamageDealt": 4544191,
"maxLargestKillingSpree": 28,
"totalPentaKills": 0,
"maxTimeSpentLiving": 1516,
"totalDeathsPerSession": 799,
"totalQuadraKills": 4,
"totalSessionsPlayed": 144,
"totalDoubleKills": 110,
"totalAssists": 1137,
"maxTimePlayed": 3951,
"mostChampionKillsPerSession": 40,
"totalDamageDealt": 23554634,
"botGamesPlayed": 0,
"killingSpree": 454,
"totalFirstBlood": 0,
"rankedSoloGamesPlayed": 0,
"totalHeal": 786891,
"totalGoldEarned": 1783556,
"mostSpellsCast": 0,
"totalTurretsKilled": 126,
"maxNumDeaths": 14,
"totalUnrealKills": 0,
"normalGamesPlayed": 0
},
"name": "Combined"
}
],
"summonerId": value
}
答案 0 :(得分:2)
你的表示法会表明你有一个对象而不是一个数组,但假设这只是一个错误的表示而你确实有一个数组,usort()
应该可以做到这一点:
$array = ...; // your starting array
usort($array, function($a, $b) {
if ($a['stats']['stats3'] == $b['stats']['stats3']) {
return 0;
} else if ($a['stats']['stats3'] > $b['stats']['stats3']) {
return 1;
} else {
return -1;
}
});
根据您更新的JSON进行更新。假设您没有在解码JSON时强制关联数组(如果您不需要,那么您真的不应该这样做):代码可能如下所示:
$object = json_decode($your_input_json);
usort($object->champions, function($a, $b) {
return $a->stats->totalSessionsPlayed - $b->stats->totalSessionsPlayed;
});
这将对您的$object->champions
数组进行排序。
答案 1 :(得分:0)
您应该将uasort()与自定义排序功能(回调)一起使用。
有关各种数组排序功能的详细信息,请阅读此http://pl1.php.net/manual/en/array.sorting.php。
答案 2 :(得分:0)
尝试使用php的usort(...)
此代码未经测试,但至少应展示usort(...)
(有关详情,请参阅documentation)。
function cmp($a, $b) {
$a = $a['stats']['totalSessionsPlayed'];
$b = $b['stats']['totalSessionsPlayed'];
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
}
usort($result['champions'], "cmp");
答案 3 :(得分:0)
您可以将uasort
与自定义回调一起使用进行排序。回调比较两个项目并返回负值,0或正值,以指示item1是否小于,等于或大于item2。
所以你可以像这样使用它:
uasort($array, function($a, $b){
return (int)($a['stats']['totalAssists']) - (int)($b['stats']['totalAssists']);
});
uasort
与usort
非常相似,但它会保留密钥。