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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7433569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:39:50  来源:igfitidea点击:

PHP Sort a multidimensional array by number of items

phparrayssorting

提问by Aviram

I have an array such as:

我有一个数组,例如:

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

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:

我想按数组内部项目的数量( 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] => 
                )
        )
)

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

任何人都可以提出一种有效的方法吗?谢谢。

回答by Arnaud Le Blanc

Using uksort:

使用uksort

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

Using array_multisort:

使用array_multisort

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


With PHP < 5.3:

使用 PHP < 5.3:

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

回答by Dan Bizdadea

<?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);
?>

回答by DaveRandom

$tempArr = $sortedArr = array();
foreach ($myArr as $k => $v) $tempArr[$k] = count($v);
asort($tempArr);
foreach ($tempArr as $k => $v) $sortedArr = $myArr[$k];

Note that this will break if any of the array values are not themselves arrays, you may want to add an is_array()check somewhere...

请注意,如果任何数组值本身不是数组,这将中断,您可能需要在is_array()某处添加检查...