作者: | 来源:互联网 | 2023-06-22 18:29
我在PHP中有这个数组:在PHPAPI中,我有这个数组,想要通过custom_price进行排序,但是没有得到如何实现…Array([0]Array([id]1204[cust
我在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