PHP按项目数对多维数组进行排序 [英] PHP Sort a multidimensional array by number of items

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

问题描述

我有一个数组,例如:

Array
(
    [DEF] => Array
        (
            [0] => Array
                (
                    [type] => 1
                    [id] => 1212
                    [name] => Jane Doe
                    [current] => 1
                )

            [1] => Array
                (
                    [type] => 1
                    [id] => 3123121
                    [name] => Door
                    [current] => 
                )
        )

    [ABC] => Array
        (
            [0] => Array
                (
                    [type] => 1
                    [id] => 1234
                    [name] => John Doe
                    [current] => 
                )
        )

    [WW] => Array
        (
            [0] => Array
                (
                    [type] => 1
                    [id] => 1212
                    [name] => Jane Doe
                    [current] => 1
                )

            [1] => Array
                (
                    [type] => 1
                    [id] => 3123121
                    [name] => Door
                    [current] => 
                )

            [2] => Array
                (
                    [type] => 1
                    [id] => 64646
                    [name] => Floor
                    [current] => 
                )
        )
)

我想按数组内部项目的数量( count() )降序(即大多数项目在前)对这个数组进行排序,所以我将拥有这个数组:

And I want to sort this array by number ( count() ) of inner-array items descending (i.e. most items first), so I will have this array:

Array
(
    [WW] => Array
        (
            [0] => Array
                (
                    [type] => 1
                    [id] => 1212
                    [name] => Jane Doe
                    [current] => 1
                )

            [1] => Array
                (
                    [type] => 1
                    [id] => 3123121
                    [name] => Door
                    [current] => 
                )

            [2] => Array
                (
                    [type] => 1
                    [id] => 64646
                    [name] => Floor
                    [current] => 
                )
        )

    [DEF] => Array
        (
            [0] => Array
                (
                    [type] => 1
                    [id] => 1212
                    [name] => Jane Doe
                    [current] => 1
                )

            [1] => Array
                (
                    [type] => 1
                    [id] => 3123121
                    [name] => Door
                    [current] => 
                )
        )

    [ABC] => Array
        (
            [0] => Array
                (
                    [type] => 1
                    [id] => 1234
                    [name] => John Doe
                    [current] => 
                )
        )
)

谁能提出一种有效的方法来做到这一点?谢谢.

Can anyone suggest an efficient way to do so? Thanks.

推荐答案

Using uksort:

Using uksort:

uksort($array, function($a, $b) { return count($b) - count($a); });

使用array_multisort:

array_multisort(array_map('count', $array), SORT_DESC, $array);

<小时>

使用 PHP <5.3:


With PHP < 5.3:

function sort_cb($a, $b) {
    return count($b) - count($a);
}
uksort($array, 'sort_cb');

这篇关于PHP按项目数对多维数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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