按键值排序php多维数组

我有下面的multidimesional数组.

Array
(
[2] => Array
    (
        [66] => Array
            (
                [id] => 66                    
                [count] => 9
            )
        [255] => Array
            (
                [id] => 255                    
                [count] => 20
            )            

    )

[1] => Array
    (
        [59] => Array
            (
                [id] => 59                
                [count] => 14
            )

        [255] => Array
            (
                [id] => 255                   
                [count] => 73
            )
    )    
)

我想按降序排列count键的内部数组.
我怎样才能实现它?

谢谢.

最佳答案 尝试以下解决方案

<?php
$array = Array
(
'2' => Array
    (
        '66' => Array
            (
                'id' => 66 ,                  
                'count' => 9
            ),
        '255' => Array
            (
                'id' => 255,                    
                'count' => 20
            )            

    ),

'1' => Array
    (
        '59' => Array
            (
                'id' => 59,                
                'count' => 14
            ),

        '255' => Array
            (
                'id' => 255,                   
                'count' => 73
            )
    )    
);

echo '<pre>';

foreach($array as &$ar){
usort($ar, function($a, $b) {
    return $b['count'] - $a['count'];
});
}

print_r($array);

输出:

Array
(
    [2] => Array
        (
            [0] => Array
                (
                    [id] => 255
                    [count] => 20
                )

            [1] => Array
                (
                    [id] => 66
                    [count] => 9
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 255
                    [count] => 73
                )

            [1] => Array
                (
                    [id] => 59
                    [count] => 14
                )

        )

)
点赞