2012-09-24 74 views
1

我有以下我想排序的多維數組。PHP Array按多個值排序

欲由total_points最裏面的數組進行排序,然後tiebraker1,2和3

實施例:

Array 
(
     [1] => Array 
       (
         [1] => Array 
           (
             [userid] => 17 
             [total_points] => 16 
             [tiebraker1] => 1 
             [tiebraker2] => 2 
             [tiebraker3] => 1 
           ) 

         [2] => Array 
           (
             [userid] => 29 
             [total_points] => 16 
             [tiebraker1] => 1 
             [tiebraker2] => 2 
             [tiebraker3] => 9 
           ) 
       ) 

     [2] => Array 
       (
         [1] => Array 
           (
             [userid] => 26 
             [total_points] => 26 
             [tiebraker1] => 2 
             [tiebraker2] => 2 
             [tiebraker3] => 4 
           ) 

         [2] => Array 
           (
             [userid] => 17 
             [total_points] => 26 
             [tiebraker1] => 3 
             [tiebraker2] => 2 
             [tiebraker3] => 4 
           ) 
       ) 
) 

結果:

Array 
(
     [1] => Array 
       (
         [1] => Array 
           (
             [userid] => 29 
             [total_points] => 16 
             [tiebraker1] => 1 
             [tiebraker2] => 2 
             [tiebraker3] => 9 
           )    
         [2] => Array 
           (
             [userid] => 17 
             [total_points] => 16 
             [tiebraker1] => 1 
             [tiebraker2] => 2 
             [tiebraker3] => 1 
           ) 

       ) 

     [2] => Array 
       (
         [1] => Array 
           (
             [userid] => 17 
             [total_points] => 26 
             [tiebraker1] => 3 
             [tiebraker2] => 2 
             [tiebraker3] => 4 
           )    
         [2] => Array 
           (
             [userid] => 26 
             [total_points] => 26 
             [tiebraker1] => 2 
             [tiebraker2] => 2 
             [tiebraker3] => 4 
           ) 


       ) 
) 

我嘗試使用在array_multisort但我不能配置它正確。

在此先感謝您的幫助!

+0

可能重複[按值排序PHP多維數組](http://stackoverflow.com/questions/2699086/php-sort-multidimensional-array-by-value) –

回答

4

要使用array_multisort您需要使用不同的數據結構。具體而言,您需要按「分數類型」進行分組(或以數學方式表示,轉置陣列)。例如。像這樣使用您的第一個例子:

array(5) { 
    // $userid 
    [0] => array(2) { 
     [0] => 17 
     [1] => 29 
    } 

    // $total_points 
    [1] => array(2) { 
     [0] => 16 
     [1] => 16 
    } 

    // $tiebreaker1 
    [2] => array(4) { 
     [0] => 1 
     [1] => 1 
    } 

    // $tiebreaker2 
    [3] => array(2) { 
     [0] => 2 
     [1] => 2 
    } 

    // $tiebreaker3 
    [4] => array(2) { 
     [0] => 1 
     [1] => 9 
    } 
} 

那麼你可以使用array_multisort()如下:

array_multisort($ar[1], SORT_DESC, SORT_NUMERIC, 
       $ar[2], SORT_DESC, SORT_NUMERIC, 
       $ar[3], SORT_DESC, SORT_NUMERIC, 
       $ar[4], SORT_DESC, SORT_NUMERIC, 
       $ar[0], SORT_ASC, SORT_NUMERIC); 

如果你不能改變陣列的結構,你可以使用usort()來代替,而手動定義比較標準。

function cmp($a, $b) 
{ 
    if ($a['total_points'] != $b['total_points']) { 
     return ($a['total_points'] > $b['total_points']) ? -1 : 1; 
    } elseif ($a['tiebreaker1'] != $b['tiebreaker1']) { 
     return ($a['tiebreaker1'] > $b['tiebreaker1']) ? -1 : 1; 
    } elseif ($a['tiebreaker2'] != $b['tiebreaker2']) { 
     return ($a['tiebraker2'] > $b['tiebreaker2']) ? -1 : 1; 
    } elseif ($a['tiebreaker3'] != $b['tiebreaker3']) { 
     return ($a['tiebreaker3'] > $b['tiebreaker3']) ? -1 : 1; 
    } else { 
     return 0; 
    } 
} 

usort($array, "cmp"); 

聲明:我不認爲我的cmp的實現是最優雅的。但它應該做的伎倆。 :)

0

從PHP.net的文檔:

<?php 
$ar = array(
     array("10", 11, 100, 100, "a"), 
     array( 1, 2, "2", 3, 1) 
    ); 
array_multisort($ar[0], SORT_ASC, SORT_STRING, 
       $ar[1], SORT_NUMERIC, SORT_DESC); 
var_dump($ar); 
?> 

在這個例子中,分選後,第一陣列將轉變爲 「10」,100,100,11, 「A」(它被分類爲字符串按升序排列)。第二個將包含1,3,「2」,2,1(按數字排序,按降序排列)。

array(2) { 
    [0]=> array(5) { 
    [0]=> string(2) "10" 
    [1]=> int(100) 
    [2]=> int(100) 
    [3]=> int(11) 
    [4]=> string(1) "a" 
    } 
    [1]=> array(5) { 
    [0]=> int(1) 
    [1]=> int(3) 
    [2]=> string(1) "2" 
    [3]=> int(2) 
    [4]=> int(1) 
    } 
} 
+0

這不適用於此問題,因爲他不想對內部數組進行排序,而是根據內部值對外部數組進行排序。 –