2011-02-23 73 views
0

想到我會首先指出我已經查看過有關Stackoverflow和互聯網的內容,雖然他們有很多示例,但他們都沒有解釋爲如何將代碼轉換爲工作方式與我的數組結構。Sorting Multidimensional Array

我相信我需要使用uksort或uasort的功能之一,但不確定這一點。

我的數組如下所示。

Array 
(
    [0] => Array 
     (
      [Result] => Array 
       (
        [id] => 260 
        [event_id] => 72 
        [year] => 2010 
        [york_score] => 27 
        [york_points] => 0.0 
        [lancs_score] => 51 
        [lancs_points] => 4.0 
        [winner] => L 
        [confirmed] => Y 
        [updated] => 2010-05-01 16:10:03 
       ) 

      [Event] => Array 
       (
        [id] => 72 
        [sport_id] => 25 
        [event_title] => 1st Team 
        [Sport] => Array 
         (
          [id] => 25 
          [sport_title] => Netball 
         ) 

       ) 

     ) 

而且它的[0]意味着它繼續。

我需要梳理所有的陣列[0,1,2,3,...]由sport_title鍵[事件]中[體育]

有誰知道如何創建一個排序功能去做這個?

如果我以後需要適配/更改代碼以在我的網站上的其他地方工作,那麼函數如何工作的一些解釋也會很有幫助。

回答

3

其中$array是數組的名稱,它包含您在問題中發佈的數組。

function sort_multi($item_1, $item_2) 
{ 
    // strcmp looks at two strings and, based off the characters' and their order, 
    // determines which one is numerically greater. When this function returns a 
    // negative, for example, it means the first item it's comparing is less that the 
    // second item (ef and eg, for example). The usort function then rearranges 
    // the array based off these comparisons. 
    return strcmp($item_1['Event']['Sport']['sport_title'], $item_2['Event']['Sport']['sport_title']); 
} 

usort($array, 'sort_multi'); 
+0

+1正是我會做的:P – alex 2011-02-23 00:50:43

+0

完美,正要爲-1表示沒有解釋,但你到底:-) – Aran 2011-02-23 00:52:08

+1

@Aran了那裏:我加了一些信息,應該是有幫助的。 – 2011-02-23 00:57:56