2011-02-23 72 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