gpt4 book ai didi

PHP - 按数组键对数组进行排序

转载 作者:行者123 更新时间:2023-12-03 20:30:03 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





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/

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