This question already has answers here:
How can I sort arrays and data in PHP?

(13个回答)


5年前关闭。




我有一个像下面提到的数组
Array
(
[6] => Array
    (
        [name] => Extras
        [total_products] => 0
        [total_sales] => 0
        [total_affiliation] => 0
    )

[5] => Array
    (
        [name] => Office Products
        [total_products] => 7
        [total_sales] => 17
        [total_affiliation] => 8
    )

[1] => Array
    (
        [name] => Hardware Parts
        [total_products] => 6
        [total_sales] => 0
        [total_affiliation] => 0
    )

)

现在订购的是:附件,办公产品,硬件零件

我想以这样的方式对主数组进行排序,即按desc顺序按inner-array的total_sales排序

因此,订单将是:办公产品,其他,硬件零件

任何帮助的人

最佳答案

PHP 5.3:

usort($array, function ($a, $b) { return $b['total_sales'] - $a['total_sales']; });

PHP 5.2-:
usort($array, create_function('$a,$b', 'return $b["total_sales"] - $a["total_sales"];'));

关于php - 基于内部数组键值的排序数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6530631/

10-14 14:41
查看更多