PHP关联数组排序

时间:2010-06-02 12:21:35

标签: php arrays sorting multidimensional-array associative-array

我有一个如下所示的数组。 目前,它按OwnerNickName字段按字母顺序排序。 现在我想使用OwnerNickName“我的房子”作为数组的第一个条目来布局数组条目,并按照OwnerNickName的字母顺序进行排序。 有什么想法吗?

Array
(
    [0318B69D-5DEB-11DF-9D7E-0026B9481364] => Array
        (
            [OwnerNickName] => andy
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => Living Room
                            [RoomKey] => FC795A73-695E-11DF-9D7E-0026B9481364
                        )

                )

        )

    [286C29DE-A9BE-102D-9C16-00163EEDFCFC] => Array
        (
            [OwnerNickName] => anton
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => KidsRoom
                            [RoomKey] => E79D7991-64DC-11DF-9D7E-0026B9481364
                        )

                    [1] => Array
                        (
                            [Label] => Basement
                            [RoomKey] => CC12C0C4-68AA-11DF-9D7E-0026B9481364
                        )

                    [2] => Array
                        (
                            [Label] => Family Room
                            [RoomKey] => 67A280D4-64D9-11DF-9D7E-0026B9481364
                        )

                )

        )

    [8BE18F84-AC22-102D-9C16-00163EEDFCFC] => Array
        (
            [OwnerNickName] => mike
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => Family Room
                            [RoomKey] => 1C6AFB39-6835-11DF-9D7E-0026B9481364
                        )

                )

        )

    [29B455DE-A9BC-102D-9C16-00163EEDFCFC] => Array
        (
            [OwnerNickName] => My House
            [Rooms] => Array
                (
                    [0] => Array
                        (
                            [Label] => Basement
                            [RoomKey] => 61ECFAB2-6376-11DF-9D7E-0026B9481364
                        )

                    [1] => Array
                        (
                            [Label] => Rec Room
                            [RoomKey] => 52B8B781-6376-11DF-9D7E-0026B9481364
                        )

                    [2] => Array
                        (
                            [Label] => Deck
                            [RoomKey] => FFEB4102-64DE-11DF-9D7E-0026B9481364
                        )

                    [3] => Array
                        (
                            [Label] => My Room2
                            [RoomKey] => 112473E4-64DF-11DF-9D7E-0026B9481364
                        )

                    [4] => Array
                        (
                            [Label] => Bar Room
                            [RoomKey] => F82C47A8-64DE-11DF-9D7E-0026B9481364
                        )

                )

        )

)

2 个答案:

答案 0 :(得分:3)

您可能希望实现自己的排序功能,例如:

function cmp($a, $b)
{
    if ($a['OwnerNickName'] == $b['OwnerNickName']) {
        return 0;
    }
    if ($a['OwnerNickName'] == 'My House') {
        return -1;
    } else if ($b['OwnerNickName'] == 'My House') {
        return 1;
    }
    return ($a['OwnerNickName'] < $b['OwnerNickName']) ? -1 : 1;
}    
usort($array, 'cmp');

答案 1 :(得分:1)

如果你想改变你对要排序的索引或哪个值应该特殊的想法,这样的事情可能适合:

function specialSort(array &$array, $index, $specialvalue){
    $callback = function($a,$b) use ($index, $specialvalue) {  //closure
       if ($a[$index] == $b[$index]) return 0;
       if ($a[$index] == $specialvalue) return -1;
       if ($b[$index] == $specialvalue) return 1;
       return ($a[$index] < $b[$index]) ? -1 : 1;
    }  ;

    uasort($array, $callback);
}

$arr=array(
    'a'=>array('thing'=>'yay','who'=>'owee'),
    'foo'=>array('thing'=>'boo','who'=>'wik'),
    'd'=>array('thing'=>'kil','who'=>'ilo'),
    'b'=>array('thing'=>'ser','who'=>'uyt'),
    'zed'=>array('thing'=>'efv','who'=>'qet')
);

specialSort($arr,'who','ilo');
print_r($arr);

给出结果:

Array
(
    [d] => Array
        (
            [thing] => kil
            [who] => ilo //special value
        )

    [a] => Array
        (
            [thing] => yay
            [who] => owee
        )

    [zed] => Array
        (
            [thing] => efv
            [who] => qet
        )

    [b] => Array
        (
            [thing] => ser
            [who] => uyt
        )

    [foo] => Array
        (
            [thing] => boo
            [who] => wik
        )

)