多维数组使用PHP排序

时间:2015-04-08 12:45:07

标签: php arrays sorting multidimensional-array

我有一个像这样的未排序数组:

Array
(
    [10] => Array
        (
            [67] => Array
                (
                    [43] => Array
                        (
                            [23] => Array
                                (
                                    [5] => 123
                                    [12] => 321
                                    [8] => 543
                                    [15] => 351
                                    [1] => 323
                                    [13] => 209
                                )

                            [65] => 123
                            [45] => 321
                            [87] => 543
                            [56] => 351
                            [1] => 323
                            [13] => 209
                        )

                    [54] => 123
                    [41] => 321
                    [4] => 543
                    [55] => 351
                    [1] => 323
                    [13] => 209
                )

            [15] => 123
            [26] => 321
            [37] => 543
            [48] => 351
            [68] => 323
            [13] => 209
        )

    [5] => Array
        (
            [43] => Array
                (
                    [23] => Array
                        (
                            [5] => 123
                            [12] => 321
                            [8] => 543
                            [15] => 351
                            [1] => 323
                            [13] => 209
                        )

                    [65] => 123
                    [45] => 321
                    [87] => 543
                    [56] => 351
                    [1] => 323
                    [13] => 209
                )

            [54] => 123
            [41] => 321
            [4] => 543
            [55] => 351
            [1] => 323
            [13] => 209
        )

    [15] => Array
        (
            [23] => Array
                (
                    [5] => 123
                    [12] => 321
                    [8] => 543
                    [15] => 351
                    [1] => 323
                    [13] => 209
                )

            [65] => 123
            [45] => 321
            [87] => 543
            [56] => 351
            [1] => 323
            [13] => 209
        )

    [25] => Array
        (
            [5] => 123
            [12] => 321
            [8] => 543
            [15] => 351
            [1] => 323
            [13] => 209
        )

)

我希望它像这样排序


Array
(
    [5] => Array
        (
            [1] => 323
            [4] => 543
            [13] => 209
            [41] => 321
            [43] => Array
                (
                    [1] => 323
                    [13] => 209
                    [23] => Array
                        (
                            [1] => 323
                            [5] => 123
                            [8] => 543
                            [12] => 321
                            [13] => 209
                            [15] => 351
                        )
                    [45] => 321
                    [56] => 351
                    [65] => 123
                    [87] => 543
                )
            [54] => 123
            [55] => 351    

        )

    [10] => Array
        (

            [13] => 209
            [15] => 123
            [26] => 321
            [37] => 543
            [48] => 351
            [67] => Array
                (
                    [1] => 323
                    [4] => 543
                    [13] => 209
                    [41] => 321
                    [43] => Array
                        (
                            [1] => 323
                            [13] => 209
                            [23] => Array
                                (
                                    [1] => 323
                                    [5] => 123
                                    [8] => 543
                                    [12] => 321
                                    [13] => 209
                                    [15] => 351
                                )
                            [45] => 321
                            [56] => 351
                            [65] => 123
                            [87] => 543                                
                        )
                    [54] => 123
                    [55] => 351
                )            
            [68] => 323

        )

    [15] => Array
        (
            [1] => 323
            [13] => 209
            [23] => Array
                (
                    [1] => 323
                    [5] => 123
                    [8] => 543
                    [12] => 321
                    [13] => 209
                    [15] => 351
                )
            [45] => 321
            [56] => 351
            [65] => 123
            [87] => 543    
        )

    [25] => Array
        (  
            [1] => 323
            [5] => 123
            [8] => 543
            [12] => 321
            [13] => 209
            [15] => 351
        )

)

我尝试了uksort,但它只对第一个数组进行排序..

uksort($m_array,'sort_function');

function sort_function($a,$b){

    if($a>$b){
        return true;
    }else if($a<$b){
        return false;
    }else{
        return 0;
    }

}

2 个答案:

答案 0 :(得分:0)

终于找到了解决方案:

这是未排序的数组


Array
(
    [10] => Array
        (
            [67] => Array
                (
                    [43] => Array
                        (
                            [23] => Array
                                (
                                    [5] => 123
                                    [12] => 321
                                    [8] => 543
                                    [15] => 351
                                    [1] => 323
                                    [13] => 209
                                )

                            [65] => 123
                            [45] => 321
                            [87] => 543
                            [56] => 351
                            [1] => 323
                            [13] => 209
                        )

                    [54] => 123
                    [41] => 321
                    [4] => 543
                    [55] => 351
                    [1] => 323
                    [13] => 209
                )

            [15] => 123
            [26] => 321
            [37] => 543
            [48] => 351
            [68] => 323
            [13] => 209
        )

    [5] => Array
        (
            [43] => Array
                (
                    [23] => Array
                        (
                            [5] => 123
                            [12] => 321
                            [8] => 543
                            [15] => 351
                            [1] => 323
                            [13] => 209
                        )

                    [65] => 123
                    [45] => 321
                    [87] => 543
                    [56] => 351
                    [1] => 323
                    [13] => 209
                )

            [54] => 123
            [41] => 321
            [4] => 543
            [55] => 351
            [1] => 323
            [13] => 209
        )

    [15] => Array
        (
            [23] => Array
                (
                    [5] => 123
                    [12] => 321
                    [8] => 543
                    [15] => 351
                    [1] => 323
                    [13] => 209
                )

            [65] => 123
            [45] => 321
            [87] => 543
            [56] => 351
            [1] => 323
            [13] => 209
        )

    [25] => Array
        (
            [5] => 123
            [12] => 321
            [8] => 543
            [15] => 351
            [1] => 323
            [13] => 209
        )

)

这是我的代码:

function recursive_sort(&$m_array) {
fs($m_array);
if(is_array($m_array)){    
    foreach($m_array as $k=> &$v){
        if (isset($v)) {
             recursive_sort($v);
         }
    }
}
}


function sortByOrder($a, $b) {
if($a>$b){
    return true;
}else if($a<$b){
    return false;
}else{
    return 0;
}
}

function fs(&$m_array){
if(is_array($m_array)){
    uksort($m_array, 'sortByOrder');
}
}

recursive_sort($m_array);

print_r($m_array);

这是输出:


Array
(
    [5] => Array
        (
            [1] => 323
            [4] => 543
            [13] => 209
            [41] => 321
            [43] => Array
                (
                    [1] => 323
                    [13] => 209
                    [23] => Array
                        (
                            [1] => 323
                            [5] => 123
                            [8] => 543
                            [12] => 321
                            [13] => 209
                            [15] => 351
                        )

                    [45] => 321
                    [56] => 351
                    [65] => 123
                    [87] => 543
                )

            [54] => 123
            [55] => 351
        )

    [10] => Array
        (
            [13] => 209
            [15] => 123
            [26] => 321
            [37] => 543
            [48] => 351
            [67] => Array
                (
                    [1] => 323
                    [4] => 543
                    [13] => 209
                    [41] => 321
                    [43] => Array
                        (
                            [1] => 323
                            [13] => 209
                            [23] => Array
                                (
                                    [1] => 323
                                    [5] => 123
                                    [8] => 543
                                    [12] => 321
                                    [13] => 209
                                    [15] => 351
                                )

                            [45] => 321
                            [56] => 351
                            [65] => 123
                            [87] => 543
                        )

                    [54] => 123
                    [55] => 351
                )

            [68] => 323
        )

    [15] => Array
        (
            [1] => 323
            [13] => 209
            [23] => Array
                (
                    [1] => 323
                    [5] => 123
                    [8] => 543
                    [12] => 321
                    [13] => 209
                    [15] => 351
                )

            [45] => 321
            [56] => 351
            [65] => 123
            [87] => 543
        )

    [25] => Array
        (
            [1] => 323
            [5] => 123
            [8] => 543
            [12] => 321
            [13] => 209
            [15] => 351
        )

)

答案 1 :(得分:0)

这是一个递归的ksort解决方案:

$m_array = [
    2 => [
        22 => [
            229 => 'i',
            221 => 'h'
        ],
        21 => 'g'
    ],
    1 => [
        12 => [
            122 => 'f',
            121 => [
                1212 => 'e',
                1211 => 'd'
            ]
        ],
        11 => [
            111 => [
                1112 => 'b',
                1119 => 'c',
                1111 => 'a'
            ]
        ],
    ],
];

function ksort_recursive(&$array) {
    if (is_array($array)) {
        array_walk($array, "ksort_recursive");
        ksort($array);
    }
}

ksort_recursive($m_array);
print_r($m_array);

输出:

Array
(
    [1] => Array
        (
            [11] => Array
                (
                    [111] => Array
                        (
                            [1111] => a
                            [1112] => b
                            [1119] => c
                        )

                )

            [12] => Array
                (
                    [121] => Array
                        (
                            [1211] => d
                            [1212] => e
                        )

                    [122] => f
                )

        )

    [2] => Array
        (
            [21] => g
            [22] => Array
                (
                    [221] => h
                    [229] => i
                )

        )

)