这个问题在这里已经有了答案:
How to Sort a Multi-dimensional Array by Value
(15 个回答)
去年关闭。
我在 PHP 中有这个数组:
在 PHP API 中,我有这个数组,想按 custom_price 排序,但不知道如何实现..
Array
(
[0] => Array
(
[id] => 1204
[custom_price] => 33.1500
)
[1] => Array
(
[id] => 1199
[custom_price] => 16.83
)
[2] => Array
(
[id] => 1176
[custom_price] => 16.83
)
[3] => Array
(
[id] => 1173
[custom_price] => 11.73
)
[4] => Array
(
[id] => 1170
[custom_price] => 22.5
)
)
我如何从 .. 高到低 & 从低到高 .. 按 custom_price 排序
最佳答案
使用 usort
:
由高到低
usort($input, function ($a, $b) {return $a['custom_price'] < $b['custom_price'];});
print_r( $input );
从低到高
usort($input, function ($a, $b) {return $a['custom_price'] > $b['custom_price'];});
print_r( $input );
http://php.net/manual/en/function.usort.php
关于PHP - 按数组键对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50636675/