基于内部数组键值的排序数组 [英] sorting array based on inner-array key-value

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

问题描述

我有一个类似于下面提到的数组

I have an array like one mentioned below

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
    )

)

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

Right now, order is: Extras, Office Products, Hardware Parts

我想按主数组的排序方式,以便按desc顺序按inner-array的total_sales排序

I want to sort main array in such as way that it is order by total_sales of inner-array in desc order

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

so order will be: Office Products, Extras, Hardware Parts

任何帮助人员

推荐答案

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"];'));

这篇关于基于内部数组键值的排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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