2

我有一个像下面的数组。 目前它按照OwnerNickName字段的字母顺序排序。 现在我想用OwnerNickName'My House'作为数组的第一个条目将数组条目作为第一个条目并按照OwnerNickName按字母顺序排序。 有什么想法?PHP关联数组排序

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 
         ) 

       ) 

     ) 

) 
+0

您是否需要永久重新排列阵列?或者是排序顺序变量? – dnagirl 2010-06-02 12:25:44

+0

永久重新订购 – 2010-06-02 12:35:47

回答

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'); 
+4

如果您想保留钥匙,可以使用uasort – 2010-06-02 12:40:46

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 
     ) 

)