gpt4 book ai didi

php - 从多维数组中排序 float

转载 作者:行者123 更新时间:2023-12-05 08:17:39 24 4
gpt4 key购买 nike

我有这个数组:

Array
(
[0] => Array
(
[company_id] => 1
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[1] => Array
(
[company_id] => 3
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[2] => Array
(
[company_id] => 4
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[3] => Array
(
[company_id] => 5
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[4] => Array
(
[company_id] => 6
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[5] => Array
(
[company_id] => 7
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[6] => Array
(
[company_id] => 11
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[7] => Array
(
[company_id] => 12
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[8] => Array
(
[company_id] => 13
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[9] => Array
(
[company_id] => 14
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[10] => Array
(
[company_id] => 15
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[11] => Array
(
[company_id] => 16
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[12] => Array
(
[company_id] => 17
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[13] => Array
(
[company_id] => 18
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[14] => Array
(
[company_id] => 19
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[15] => Array
(
[company_id] => 20
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[16] => Array
(
[company_id] => 21
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[17] => Array
(
[company_id] => 22
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[18] => Array
(
[company_id] => 23
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[19] => Array
(
[company_id] => 25
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[20] => Array
(
[company_id] => 26
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[21] => Array
(
[company_id] => 27
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[22] => Array
(
[company_id] => 28
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[23] => Array
(
[company_id] => 29
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[24] => Array
(
[company_id] => 30
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[25] => Array
(
[company_id] => 31
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[26] => Array
(
[company_id] => 32
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[27] => Array
(
[company_id] => 33
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[28] => Array
(
[company_id] => 34
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[29] => Array
(
[company_id] => 35
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[30] => Array
(
[company_id] => 36
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[31] => Array
(
[company_id] => 37
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[32] => Array
(
[company_id] => 38
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[33] => Array
(
[company_id] => 39
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[34] => Array
(
[company_id] => 40
[avg_company] => 4.00000
[avg_product] => 2.00000
[avg_service] => 5.00000
[avg_price] => 5.00000
[avg_rating_remark] => Solid
[avg_rating] => 4.0
)

[35] => Array
(
[company_id] => 41
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[36] => Array
(
[company_id] => 42
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[37] => Array
(
[company_id] => 43
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[38] => Array
(
[company_id] => 44
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[39] => Array
(
[company_id] => 45
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[40] => Array
(
[company_id] => 46
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[41] => Array
(
[company_id] => 47
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[42] => Array
(
[company_id] => 48
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[43] => Array
(
[company_id] => 49
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[44] => Array
(
[company_id] => 50
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[45] => Array
(
[company_id] => 51
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[46] => Array
(
[company_id] => 52
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[47] => Array
(
[company_id] => 53
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[48] => Array
(
[company_id] => 55
[avg_company] => 4.50000
[avg_product] => 4.50000
[avg_service] => 4.50000
[avg_price] => 3.00000
[avg_rating_remark] => Solid
[avg_rating] => 4.1
)

[49] => Array
(
[company_id] => 58
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[50] => Array
(
[company_id] => 61
[avg_company] => 5.00000
[avg_product] => 5.00000
[avg_service] => 5.00000
[avg_price] => 5.00000
[avg_rating_remark] => Excellent
[avg_rating] => 5.0
)

[51] => Array
(
[company_id] => 62
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)
)

我想按键值 avg_rating 降序排序,所以我根据其他 SO 问题使用它:

usort($ret, function($a, $b) {
return $a['avg_rating'] - $b['avg_rating'];
});

但是结果不对:

Array
(
[0] => Array
(
[company_id] => 39
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[1] => Array
(
[company_id] => 38
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[2] => Array
(
[company_id] => 41
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[3] => Array
(
[company_id] => 42
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[4] => Array
(
[company_id] => 43
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[5] => Array
(
[company_id] => 37
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[6] => Array
(
[company_id] => 36
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[7] => Array
(
[company_id] => 32
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[8] => Array
(
[company_id] => 33
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[9] => Array
(
[company_id] => 34
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[10] => Array
(
[company_id] => 35
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[11] => Array
(
[company_id] => 44
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[12] => Array
(
[company_id] => 45
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[13] => Array
(
[company_id] => 52
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[14] => Array
(
[company_id] => 53
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[15] => Array
(
[company_id] => 58
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[16] => Array
(
[company_id] => 62
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[17] => Array
(
[company_id] => 51
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[18] => Array
(
[company_id] => 50
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[19] => Array
(
[company_id] => 46
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[20] => Array
(
[company_id] => 47
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[21] => Array
(
[company_id] => 48
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[22] => Array
(
[company_id] => 49
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[23] => Array
(
[company_id] => 1
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[24] => Array
(
[company_id] => 31
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[25] => Array
(
[company_id] => 13
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[26] => Array
(
[company_id] => 12
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[27] => Array
(
[company_id] => 14
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[28] => Array
(
[company_id] => 15
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[29] => Array
(
[company_id] => 16
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[30] => Array
(
[company_id] => 11
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[31] => Array
(
[company_id] => 7
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[32] => Array
(
[company_id] => 3
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[33] => Array
(
[company_id] => 4
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[34] => Array
(
[company_id] => 5
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[35] => Array
(
[company_id] => 6
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[36] => Array
(
[company_id] => 30
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[37] => Array
(
[company_id] => 17
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[38] => Array
(
[company_id] => 26
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[39] => Array
(
[company_id] => 25
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[40] => Array
(
[company_id] => 27
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[41] => Array
(
[company_id] => 28
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[42] => Array
(
[company_id] => 29
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[43] => Array
(
[company_id] => 18
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[44] => Array
(
[company_id] => 23
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[45] => Array
(
[company_id] => 20
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[46] => Array
(
[company_id] => 22
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[47] => Array
(
[company_id] => 19
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[48] => Array
(
[company_id] => 21
[avg_company] =>
[avg_product] =>
[avg_service] =>
[avg_price] =>
[avg_rating] => 0
)

[49] => Array
(
[company_id] => 61
[avg_company] => 5.00000
[avg_product] => 5.00000
[avg_service] => 5.00000
[avg_price] => 5.00000
[avg_rating_remark] => Excellent
[avg_rating] => 5.0
)

[50] => Array
(
[company_id] => 55
[avg_company] => 4.50000
[avg_product] => 4.50000
[avg_service] => 4.50000
[avg_price] => 3.00000
[avg_rating_remark] => Solid
[avg_rating] => 4.1
)

[51] => Array
(
[company_id] => 40
[avg_company] => 4.00000
[avg_product] => 2.00000
[avg_service] => 5.00000
[avg_price] => 5.00000
[avg_rating_remark] => Solid
[avg_rating] => 4.0
)
)

最佳答案

我在这个 question 上应用了解决方案,将我的 usort() 语法替换为:

usort($ret, function($a, $b) {
$a = $a['avg_rating'];
$b = $b['avg_rating'];
if ($a == $b) { return 0; }
return ($a < $b) ? -1 : 1;
});

然后我在 $ret 上执行 array_reverse():

$ret = array_reverse($ret);

关于php - 从多维数组中排序 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14118105/

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