如何通过内部键对多维数组进行排序

我有一个巨大的数组,我是从 BattleField Bad Company 2 的 API 中提取的,士兵统计数据可以作为一个多维数组提取,每个士兵都有一个内部数组,但是 API 将士兵排序按字母顺序命名,我想按等级对它们进行排序(这只是该士兵数组中的另一个键).几天来我一直在努力解决这个问题,有人有任何想法吗?(即按 $arr[players][][rank]

对数组进行排序

这里有一些数组

<前>大批([玩家] => 数组([0] => 数组([名称] => bigjay517[排名] => 29[rank_name] => II 少尉[老兵] => 0[得分] => 979440[级别] => 169[杀死] => 4134[死亡人数] => 3813[时间] => 292457.42[elo] => 319.297[形式] => 1[date_lastupdate] => 2010-03-30T14:06:20+02:00[count_updates] => 13[通用] => 数组([准确度] => 0.332[狗] => 86[狗] => 166[elo0] => 309.104[elo1] => 230.849[游戏] => 384[黄金版] => 0[损失] => 161[sc_assault] => 146333[sc_award] => 567190[sc_bonus] => 35305[sc_demo] => 96961[sc_general] => 264700[sc_objective] => 54740[sc_recon] => 54202[sc_squad] => 53210[sc_support] => 70194[sc_team] => 21215[sc_vehicle] => 44560[等级] => 0[spm] => 0[spm0] => 0[spm1] => 0[srank] => 0[资深] => 0[团队杀戮] => 67[udogt] => 0[获胜] => 223)

解决方案

除了其他答案,如果您需要按动态字段排序(仅在运行时已知),您可以使用匿名函数并将其传递给字段通过 use 关键字:

$field = "some_dynamic_value";usort($rows, function($a, $b) use ($field) {返回 strcmp($a[$field], $b[$field]);});

i have this enormous array that i am pulling from an API for BattleField Bad Company 2, and the soldier stats can be pulled as a multi dimensional array with an inner array for each soldier, however the API sormats it sorting the soldiers by name alphabetically, i want to sort them by rank (which is just another key within that soldiers array). ive been trying to figure this out for days, anyone have any ideas? (ie sort the array by $arr[players][][rank]

here is a bit of the array

Array
(
    [players] => Array
        (
            [0] => Array
                (
                    [name] => bigjay517
                    [rank] => 29
                    [rank_name] => SECOND LIEUTENANT II
                    [veteran] => 0
                    [score] => 979440
                    [level] => 169
                    [kills] => 4134
                    [deaths] => 3813
                    [time] => 292457.42
                    [elo] => 319.297
                    [form] => 1
                    [date_lastupdate] => 2010-03-30T14:06:20+02:00
                    [count_updates] => 13
                    [general] => Array
                        (
                            [accuracy] => 0.332
                            [dogr] => 86
                            [dogt] => 166
                            [elo0] => 309.104
                            [elo1] => 230.849
                            [games] => 384
                            [goldedition] => 0
                            [losses] => 161
                            [sc_assault] => 146333
                            [sc_award] => 567190
                            [sc_bonus] => 35305
                            [sc_demo] => 96961
                            [sc_general] => 264700
                            [sc_objective] => 54740
                            [sc_recon] => 54202
                            [sc_squad] => 53210
                            [sc_support] => 70194
                            [sc_team] => 21215
                            [sc_vehicle] => 44560
                            [slevel] => 0
                            [spm] => 0
                            [spm0] => 0
                            [spm1] => 0
                            [srank] => 0
                            [sveteran] => 0
                            [teamkills] => 67
                            [udogt] => 0
                            [wins] => 223
                        )

解决方案

In addition to the other answers, if you need to sort by a dynamic field (only known at runtime), you can use an anonymous function and pass it the field via the use keyword:

$field = "some_dynamic_value";

usort($rows, function($a, $b) use ($field) {
    return strcmp($a[$field], $b[$field]);
});

相关文章