2011-01-26 81 views
1

我想通過season_number排序這個數組但我不確定使用哪個函數,因爲我假設我需要自定義排序?有任何想法嗎?排序多維數組幫助

Array 
(
    [0] => Array 
     (
      [season_number] => 7 
      [show_id] => 21 
      [show_seasons_id] => 14 
     ) 

    [1] => Array 
     (
      [season_number] => 6 
      [show_id] => 21 
      [show_seasons_id] => 31 
     ) 

    [2] => Array 
     (
      [season_number] => 1 
      [show_id] => 21 
      [show_seasons_id] => 40 
     ) 

    [3] => Array 
     (
      [season_number] => 2 
      [show_id] => 21 
      [show_seasons_id] => 41 
     ) 
) 

回答

1

您可以使用usort功能與「比較」功能:

function compare_my_elements($arr1, $arr2) { 
    $s1=$arr1["season_number"]; 
    $s2=$arr2["season_number"]; 
    if($s1 == $s2) return 0; 
    return ($s1 > $s2 ? 1 : -1); 
} 

usort($my_md_array, compare_my_elements); 
1

試試這個:

foreach ($array as $key => $val) { 
    $newArr[$key] = $val['season_number']; 
} 
array_multisort($newArr, SORT_ASC, $array); 

其中$數組是你打印出數組。

+0

好的一個......雖然`$ link`可能將不得不成爲`$ val`。 – xtofl 2011-01-26 16:19:18