PHP按项目数排序多维数组

时间:2011-09-15 15:39:20

标签: php arrays sorting

我有一个数组如:

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

有人能建议一种有效的方法吗?感谢。

3 个答案:

答案 0 :(得分:32)

使用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');

答案 1 :(得分: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);
?>

答案 2 :(得分:1)

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

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