2010-06-02 255 views
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 
     ) 

)