PHP 5:按嵌套值排序多维数组,或者有条件地排序第二个嵌套值

时间:2013-04-19 18:31:49

标签: php arrays multidimensional-array sorting

我认为这很简单,我只是想弄清楚如何在脑海中完成这项工作。

问题的一般形式如下:

<?php 
$array_to_sort(
           0 => array(
                     'element1' => array(),
                     'PrimarySortKey' => int
                     'element2' => array(
                                        'SecondarySortKey' => int
                                         )
                     )
           1 => array(
                     'element1' => array(),
                     'PrimarySortKey' => int
                     'element2' => array(
                                        'SecondarySortKey' => int
                                         )
                     )

           n => array(
                     'element1' => array(),
                     'PrimarySortKey' => int
                     'element2' => array(
                                        'SecondarySortKey' => int
                                         )
                     )
           );
?>

如果此模型中不明显,则目标是按PrimarySortKey排序,除非它们被证明是相等的,在这种情况下按SecondarySortKey排序。

阅读PHP手册给我留下的印象是,这将是array_multisort()array_walk()函数的一些复杂实例,但我永远无法理解这类事情。 :/

我不是要求任何人为我写这个功能,但我很感激这种方法的帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

请查看函数PHP usort

工作示例:

$arr = array(
        array('PrimarySortKey'=>30,'SecondarySortKey'=>3),
        array('PrimarySortKey'=>1,'SecondarySortKey'=>1),
        array('PrimarySortKey'=>30,'SecondarySortKey'=>9)
    );

    print_r($arr);

    usort($arr,function($a,$b){
       $key_a = $a['PrimarySortKey'];
       $key_b = $b['PrimarySortKey'];
        if($key_a == $key_b){
            $s_key_a = $a['SecondarySortKey'];   
            $s_key_b = $b['SecondarySortKey'];   

            return ($s_key_a < $s_key_b) ? -1 : 1;      
        }
        else{
            return ($key_a < $key_b) ? -1 : 1;      
        }

    });

    print_r($arr);

打印:

Array
(
    [0] => Array
        (
            [PrimarySortKey] => 30
            [SecondarySortKey] => 3
        )

    [1] => Array
        (
            [PrimarySortKey] => 1
            [SecondarySortKey] => 1
        )

    [2] => Array
        (
            [PrimarySortKey] => 30
            [SecondarySortKey] => 9
        )

)
Array
(
    [0] => Array
        (
            [PrimarySortKey] => 1
            [SecondarySortKey] => 1
        )

    [1] => Array
        (
            [PrimarySortKey] => 30
            [SecondarySortKey] => 3
        )

    [2] => Array
        (
            [PrimarySortKey] => 30
            [SecondarySortKey] => 9
        )

)

答案 1 :(得分:1)

使用usort()的实现如下:

function cmp($a, $b) {
    if ($a['PrimarySortKey'] == $b['PrimarySortKey']) {
        if ($a['element2']['SecondarySortKey'] == $b['element2']['SecondarySortKey']) {
           return 0;
        }
        return ($a['element2']['SecondarySortKey'] < $b['element2']['SecondarySortKey']) ? -1 : 1;
    }
    return ($a['PrimarySortKey'] < $b['PrimarySortKey']) ? -1 : 1;
}

usort($array_to_sort, 'cmp');