gpt4 book ai didi

php - 按特定顺序对数组进行排序

转载 作者:行者123 更新时间:2023-12-03 22:44:59 24 4
gpt4 key购买 nike

我有以下数组

Array
(
[0] => Array
(
[label] => Germany
[conversions] => 1
)

[1] => Array
(
[label] => United States
[conversions] => 8
)

[2] => Array
(
[label] => France
[conversions] => 1
)

[3] => Array
(
[label] => China
[conversions] => 1
)

[4] => Array
(
[label] => Philippines
[conversions] => 1
)

[5] => Array
(
[label] => Turkey
[conversions] => 1
)

)

我想对以下数组进行排序,首先通过转换(desc),然后通过标签(asc)

输出将具有以下顺序:

United States
China
France
Germany
Philippines
Turkey

最佳答案

如果使用 >= PHP 5.3...

usort($arr, function($a, $b) {

$diff = $b['conversions'] - $a['conversions'];

if ( ! $diff) {
return strcmp($a['label'], $b['label']);
}

return $diff;

});

使其与< PHP 5.3 一起工作相对简单。

关于php - 按特定顺序对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6732438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com