我目前正在尝试按其子值对多维数组进行排序。数组的结构是:
[0] => Array
(
[id] => 87
[sold] => 50
[stock] => 991
[speed] => 1.5
[days_left] => 660.66666666667
)
[1] => Array
(
[id] => 97
[sold] => 20
[stock] => 120
[speed] => 1.2
[days_left] => 100
)
[2] => Array
(
[id] => 36
[sold] => 2
[stock] => 1020
[speed] => 1.02
[days_left] => 1000
)
我正在使用的代码是:
usort($data, function($a, $b) { return $a[$_GET['sortby']] - $b[$_GET['sortby']]; });
其中 $_GET['sortby'] 变量等于键。
到目前为止一切顺利,一切正常,除了速度之外,它对所有值进行了正确排序!首先,我认为它与小数有关,但 days_left 也包括小数并且排序正确..:/
正确的输出(days_left):
[0] => Array
(
[id] => 97
[sold] => 20
[stock] => 120
[speed] => 1.2
[days_left] => 100
)
[1] => Array
(
[id] => 87
[sold] => 50
[stock] => 991
[speed] => 1.5
[days_left] => 660.66666666667
)
[2] => Array
(
[id] => 36
[sold] => 2
[stock] => 1020
[speed] => 1.02
[days_left] => 1000
)
错误输出(速度):
[0] => Array
(
[id] => 97
[sold] => 20
[stock] => 120
[speed] => 1.2
[days_left] => 100
)
[1] => Array
(
[id] => 87
[sold] => 50
[stock] => 991
[speed] => 1.5
[days_left] => 660.66666666667
)
[2] => Array
(
[id] => 36
[sold] => 2
[stock] => 1020
[speed] => 1.02
[days_left] => 1000
)
希望有人可以帮助我!