PHP-按数组键对数组进行排序 [英] PHP - sort array by array key

查看:117
本文介绍了PHP-按数组键对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中具有以下数组:

I have this array in PHP:

在PHP API中,我有此数组,并希望按custom_price排序ot,但不知道如何实现..

In PHP APIs I have this array and want to sort ot by custom_price, but not getting how to acheive so ..

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

How i can sort from .. high to low & low to high .. by 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-按数组键对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆