PHP 按项目数对多维数组进行排序

2022-08-30 15:17:30

我有一个数组,例如:

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())对这个数组进行排序,所以我将有这个数组:

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] => 
                )
        )
)

任何人都可以提出一种有效的方法来做到这一点吗?谢谢。


答案 1

使用 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:

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

答案 2
<?php
function cmp($a, $b)
{
    if ($a == $b) {
        return 0;
    }
    return (count($a) > count($b)) ? -1 : 1;
}

$a = array(
"AA" => array(
        array('type'=>'1', 'id'=>'2'),
        array('type'=>'2', 'id'=>'2')),
'BB' => array(
        array('type'=>'1', 'id'=>'2'),
        array('type'=>'2', 'id'=>'2'),
        array('type'=>'5', 'id'=>'2')),
'CC' => array(
        array('type'=>'1', 'id'=>'2'))
);  

usort($a, "cmp");

print_r($a);
?>