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

时间:2021-11-03 21:32:50

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]

我有一个庞大的阵列,我从BattleField Bad Company 2的API中提取,并且士兵统计数据可以作为一个多维数组拉出来,每个士兵都有一个内部阵列,但是API会让它按照字母顺序按名称对士兵进行排序,我想按等级对它们进行排序(这只是士兵阵列中的另一个关键)。我一直试图解决这个问题几天,任何人都有任何想法?(即按$ arr [球员] [] [排名]排序数组

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                        )

6 个解决方案

#1


You can sort any array by any criteria using usort()

您可以使用usort()按任何条件对任何数组进行排序

#2


Here you go.

干得好。

$playergoop is the array that you provided.

$ playergoop是您提供的数组。

This one sorts by the sub-field 'rank', but it does so in an ascending order. If you want a descending order, you can switch the > to <.

这个按子字段“等级”排序,但它按升序排序。如果您想要降序,可以将>切换为<。

function sorter($one, $two) {    return ($one['rank'] > $two['rank']);}usort($playergoop['players'], sorter);

#3


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:

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

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

#4


When you're using PHP 5.3 and above you can use an anonymous inline function for sorting:

当您使用PHP 5.3及更高版本时,您可以使用匿名内联函数进行排序:

usort($obj, function ($a, $b){    return strcmp($a["name"], $b["name"]);});

#5


To order descending an array, I used

为了命令降序数组,我用了

function sorterdesc($one, $two) {    return ($two['cont'] - $one['cont']);}

For ascending :

升序:

function sorterasc($one, $two) {    return ($one['cont'] - $two['cont']);}

Like this it works fine with numeric values

像这样它可以正常使用数值

#6


You can use array_multisort for this: first provide the column you want to sort -- with array_column and as second argument the whole array. There are several options possible (see documentation), but for an ascending sort by the "rank" field, it would look like this:

您可以使用array_multisort:首先提供要排序的列 - 使用array_column,并将第二个参数作为整个数组。有几种选择可能(参见文档),但对于“rank”字段的升序排序,它看起来像这样:

array_multisort(array_column($players, "rank"), $players);

NB: For the data in the actual question here, $players would be &$arr["players"]

注意:对于这里实际问题中的数据,$玩家将是&$ arr [“玩家”]

#1


You can sort any array by any criteria using usort()

您可以使用usort()按任何条件对任何数组进行排序

#2


Here you go.

干得好。

$playergoop is the array that you provided.

$ playergoop是您提供的数组。

This one sorts by the sub-field 'rank', but it does so in an ascending order. If you want a descending order, you can switch the > to <.

这个按子字段“等级”排序,但它按升序排序。如果您想要降序,可以将>切换为<。

function sorter($one, $two) {    return ($one['rank'] > $two['rank']);}usort($playergoop['players'], sorter);

#3


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:

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

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

#4


When you're using PHP 5.3 and above you can use an anonymous inline function for sorting:

当您使用PHP 5.3及更高版本时,您可以使用匿名内联函数进行排序:

usort($obj, function ($a, $b){    return strcmp($a["name"], $b["name"]);});

#5


To order descending an array, I used

为了命令降序数组,我用了

function sorterdesc($one, $two) {    return ($two['cont'] - $one['cont']);}

For ascending :

升序:

function sorterasc($one, $two) {    return ($one['cont'] - $two['cont']);}

Like this it works fine with numeric values

像这样它可以正常使用数值

#6


You can use array_multisort for this: first provide the column you want to sort -- with array_column and as second argument the whole array. There are several options possible (see documentation), but for an ascending sort by the "rank" field, it would look like this:

您可以使用array_multisort:首先提供要排序的列 - 使用array_column,并将第二个参数作为整个数组。有几种选择可能(参见文档),但对于“rank”字段的升序排序,它看起来像这样:

array_multisort(array_column($players, "rank"), $players);

NB: For the data in the actual question here, $players would be &$arr["players"]

注意:对于这里实际问题中的数据,$玩家将是&$ arr [“玩家”]