Nar*_*yas 5 php arrays sorting
我在 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
)
)
Run Code Online (Sandbox Code Playgroud)
我如何从 .. 高到低 & 从低到高 .. 按 custom_price 排序
使用usort:
从高到低
usort($input, function ($a, $b) {return $a['custom_price'] < $b['custom_price'];});
print_r( $input );
Run Code Online (Sandbox Code Playgroud)
从低到高
usort($input, function ($a, $b) {return $a['custom_price'] > $b['custom_price'];});
print_r( $input );
Run Code Online (Sandbox Code Playgroud)
http://php.net/manual/en/function.usort.php
| 归档时间: |
|
| 查看次数: |
10886 次 |
| 最近记录: |