2013-03-12 266 views
0

我有一個數組,看起來像這樣:PHP排序多維數組

Array 
(
[13] => Array 
     (
      [name] => Blah blah 
      [description] => Blah blah blah 
      [parent_group_id] => 8 
      [display] => Blah : Blah 
      [stamps] => Array 
       (
        [73] => Array 
         (
          [stamp_id] => 73 
          [name] => Blah 
          [is_member] => 
          [rank] => 2   <-- 
          [description] => Blah blah blah 
          [rank_token] => easy 
         ) 
        [80] => Array 
         (
          [stamp_id] => 80 
          [name] => Blah 
          [is_member] => 
          [rank] => 1   <-- 
          [description] => Blah blah blah 
          [rank_token] => medium 
         ) 
        [73] => Array 
         (
          [stamp_id] => 73 
          [name] => Blah 
          [is_member] => 
          [rank] => 4   <-- 
          [description] => Blah blah blah 
          [rank_token] => easy 
         ) 

        [80] => Array 
         (
          [stamp_id] => 80 
          [name] => Blah 
          [is_member] => 
          [rank] => 3   <-- 
          [description] => Blah blah blah 
          [rank_token] => medium 
         ) 
       ) 
     ) 
) 

然而,我要通過[「秩」]保持它們是1,2中的數字時,這個陣列中的對象進行排序,3和4.(我在代碼示例中添加了箭頭)因此,在使用usort之後,我希望所有內容都按數字順序排序。所以我希望它看起來像這樣:

Array 
(
[13] => Array 
     (
      [name] => Blah blah 
      [description] => Blah blah blah 
      [parent_group_id] => 8 
      [display] => Blah : Blah 
      [stamps] => Array 
       (
        [80] => Array 
         (
          [stamp_id] => 80 
          [name] => Blah 
          [is_member] => 
          [rank] => 1   <-- 
          [description] => Blah blah blah 
          [rank_token] => medium 
         ) 
        [73] => Array 
         (
          [stamp_id] => 73 
          [name] => Blah 
          [is_member] => 
          [rank] => 2   <-- 
          [description] => Blah blah blah 
          [rank_token] => easy 
         ) 
        [80] => Array 
         (
          [stamp_id] => 80 
          [name] => Blah 
          [is_member] => 
          [rank] => 3   <-- 
          [description] => Blah blah blah 
          [rank_token] => medium 
         ) 
        [73] => Array 
         (
          [stamp_id] => 73 
          [name] => Blah 
          [is_member] => 
          [rank] => 4   <-- 
          [description] => Blah blah blah 
          [rank_token] => easy 
         ) 
       ) 
     ) 
) 

我的實際數組要大得多,但它仍然遵循這種模式。

+0

我認爲這將有助於: michi 2013-03-12 21:59:46

+1

你剛纔提到'usort',你試過了什麼? 'foreach($ array as $ key => $ value)usort($ array [$ key] ['stamps'],'yourusortfunction');' – Wrikken 2013-03-12 22:00:13

回答

1

這應該工作,如果我理解正確你的問題:

function cmp($a, $b) 
{ 
    if ($a['rank'] == $b['rank']) { 
     return 0; 
    } 
    return ($a['rank'] < $b['rank']) ? -1 : 1; 
} 
foreach($yourArray as &$entry) { 
    uasort($entry['stamps'], "cmp"); 
} 
unset($entry); 

print_r($yourArray); 

注意,CMP功能幾乎是一樣的說明書中無。 foreach中的&符號意味着創建的變量是數組成員的別名,而不是副本(正如PHP通常所做的那樣)。 unset()在那裏,因爲如果你以後嘗試使用一個名爲$ entry的變量,你實際上會操作數組中的最後一個條目。

如果您對此感到不舒服,還有其他方法可以對其進行剝皮;例如,您可以創建第二個功能,它array_map你原來的,即:

function cmp($a, $b) 
{ 
    if ($a['rank'] == $b['rank']) { 
     return 0; 
    } 
    return ($a['rank'] < $b['rank']) ? -1 : 1; 
} 
function sort_entry($entry) 
{ 
    uasort($entry['stamps'], "cmp"); 
    return $entry; 
} 
array_map('sort_entry', $yourArray);  
print_r($yourArray); 
+0

像魅力一樣工作,非常感謝! – user2154729 2013-03-12 22:22:54

+0

沒有問題,祝你有個愉快的夜晚 – Matt 2013-03-12 22:26:23