微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP – 按数组键排序数组

我在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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐