2012-12-05 245 views
0

我有以下數組,我想按總數降序排列所有記錄。任何人都可以告訴我這樣做的PHP代碼或僞代碼。使用索引對多維數組排序多維數組

Array 
(
    [reg_id] => Array 
     (
      [0] => 5 
      [1] => 7 
      [2] => 17 
      [3] => 18 
      [4] => 19 
      [5] => 20 
      [6] => 34 
     ) 

    [name] => Array 
     (
      [0] => employee1 
      [1] => employee6 
      [2] => employee3 
      [3] => employee4 
      [4] => employee2 
      [5] => empoyee5 
      [6] => employee9 
     ) 

    [test_w] => Array 
     (
      [0] => 21/30 
      [1] => 15/30 
      [2] => 27/30 
      [3] => 16.5/30 
      [4] => 21/30 
      [5] => 18/30 
      [6] => 12/30 
     ) 

    [intr_w] => Array 
     (
      [0] => 8/10 
      [1] => 6/10 
      [2] => 9/10 
      [3] => 9/10 
      [4] => 3.3/10 
      [5] => 7/10 
      [6] => 0/10 
     ) 

    [exp_w] => Array 
     (
      [0] => 2.5/5 
      [1] => 4/5 
      [2] => 4.35/5 
      [3] => 4.5/5 
      [4] => 4.8/5 
      [5] => 4.5/5 
      [6] => 0/5 
     ) 

    [educ_w] => Array 
     (
      [0] => 37.41/55 
      [1] => 44.14/55 
      [2] => 33.27/55 
      [3] => 38.43/55 
      [4] => 34.52/55 
      [5] => 46.11/55 
      [6] => 43.66/55 
     ) 

    [total] => Array 
     (
      [0] => 68.91 
      [1] => 69.14 
      [2] => 73.62 
      [3] => 68.43 
      [4] => 63.62 
      [5] => 75.61 
      [6] => 55.66 
     ) 

) 
+0

[array_multisort()](http://php.net/manual/en/array.sorting.php)? – GolezTrol

+0

你會把這個數組變成一維數組嗎? –

+0

不,怎麼做? –

回答

1

使用該函數應該解決的問題

function multisort($array, $key, $sort_flags = SORT_REGULAR) { 
     if (is_array($array) && count($array) > 0) { 
      if (!empty($key)) { 
       $mapping = array(); 
       foreach ($array as $k => $v) { 
        $sort_key = ''; 
        if (!is_array($key)) { 
         $sort_key = $v[$key]; 
        } else { 
         // @TODO This should be fixed, now it will be sorted as string 
         foreach ($key as $key_key) { 
          $sort_key .= $v[$key_key]; 
         } 
         $sort_flags = SORT_STRING; 
        } 
        $mapping[$k] = $sort_key; 
       } 
       asort($mapping, $sort_flags); 
       $sorted = array(); 
       foreach ($mapping as $k => $v) { 
        $sorted[] = $array[$k]; 
       } 
       return $sorted; 
      } 
     } 
     return $array;  

} 

例如:

$result=multisort($array,$sort_flags = DESC); 
1

最簡單的(但至少有效)將是一個簡單的冒泡排序

僞代碼

for($i from 0 to count($arr[total])): 
     for($j from 0 to count(...)): 
      if($arr[total][$i] < $arr[total][$j]): 
        //SWAP VALUES AT i AND j 
        $this->swap($arr[total][$i],$arr[total][$j]); 
        $this->swap($arr[name][$i],$arr[name][$j]); 
        $this->swap($arr[reg_id][$i],$arr[reg_id][$j]); 
        // etc for all required fields 
      endif; 
     endfor; 
endfor; 

交換功能(PASS珍視參考,因此,他們將得到改變)

function swap(&$i, &$j){ 
    $t=$i; 
    $i=$j; 
    $j=$t; 
} 
+0

你能否解釋一下術語將所有字段替換爲我的情況。我堅持這一點。 –

+0

我編輯了上面的答案。它現在包含你如何交換。再次,這可以用於任何排序算法,不僅是Bubble排序 – cjds

0

由於這些似乎是與實體相關的領域,最好的答案我可以給你是爲了建立更好的數據結構。舉例,你應該建立包含這些領域的員工(或任何實體您代表那裏)類:

class Employee 
{ 
public $reg_id; 
public $name; 
public $test_w; 
public $intr_w; 
public $exp_w; 
public $educ_w; 
public $total; 
} 

,然後保持員工的數組。如果你有另一個領域又是一個數組呢?我知道我的答案沒有解決實際的分類問題,我只是說你保持數據的方式非常快速。

0

我有以下功能排序數組。

function msort($array, $key_s) { 
    $arraynew = $array; 
    foreach($array as $key=>$val){ 
     if($key == $key_s){ 

      for($i=0; $i<count($val); $i++){ 
       for($j=0; $j<count($val); $j++){ 
        if($val[$i] > $val[$j]){ 
         $temp = $val[$i]; 
         $val[$i] = $val[$j]; 
         $val[$j] = $temp; 

         // swap all values 
         foreach($arraynew as $arrKey=>$arrVal){ 
          $temp = $arraynew[$arrKey][$i]; 
          $arraynew[$arrKey][$i] = $arraynew[$arrKey][$j]; 
          $arraynew[$arrKey][$j] = $temp; 
         } 
        } 
       } 
      } 
     } 

    } 
    return $arraynew; 
}