php - 基于内部数组键值对数组排序

标签 php

<分区>

我有一个像下面提到的数组

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
    )

)

现在,订单是:配件、办公用品、硬件零件

我想对主数组进行排序,使其按降序排列的内部数组的 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/

相关文章:

php - 使用 MySQL 数据库中的记录作为值创建复选框列表

php - MySQL:计算每个字段中的不同值

php - 添加时间时更改日期

php - Stripe webhook 测试返回 http 500 错误

php - 使用 Jquery 从字符串下载 pdf 文件

php - 按位标志被放弃了吗?

php - 我动态放置的内容显示不正确

PHP,一个奇怪的变量范围?

php - 从同一行中选择多个值?

php - 如何用php制作缩略图