2010-08-11 24 views
3

我有一個數字數組,例如myarray =(45,3,56,7,21)。我需要做的是將這些值排列到另一個數組中,因此對於上述情況,我最終會以myarray2 =(4,1,5,2,3)結束。如何給隊列分配一個等級號碼

非常感謝,

亞當

+0

任何人都可以建議你如何解決這個問題,但按降序排列數組? – ajcw 2012-01-29 21:18:01

回答

2

你去那裏,完整的解決方案:

<?php 

$myarray = array(45,3,56,7,21); 

//create a copy and sort 
$myarray_copy = $myarray; 
sort($myarray_copy); 

//reverses key and values 
$myarray_copy = array_flip($myarray_copy); 

//create result by using keys from sorted values + 1 
foreach($myarray as $val) 
    $myarray2[] = $myarray_copy[$val]+1; 

//print final array 
print_r($myarray2); 

/* 
Output: 

Array ([0] => 4 [1] => 1 [2] => 5 [3] => 2 [4] => 3) 
*/ 
?> 
0

採取了一些你的陣列中,算多少數少於一個小時,添加一個和你有秩。

1

非常有趣。

我可以拿出當場什麼是這樣的:

排序數組並將它放入另一個數組,然後通過搜索中間數組中的值,然後填寫myarray2的填寫myarray2值與索引。

$myarraytemp = sort clone $myarray; 

foreach ($myarray as $num) 
{ 
    array_push($myarray2, array_search($num,$myarraytemp)); 
} 

或者你可以這樣做:

foreach ($myarray as $num) 
{ 
    $rank=0; 
    foreach ($myarray as $innernum) 
    { 
     if($innernum<= $num) 
      $rank++;    
    } 
    array_push($myarray2, $rank); 
} 
+0

如果你注意到這裏有任何微小的語法錯誤,請讓我知道,最近做了很多perl,所以一些perl語法可能會意外地流入我的php。 – 2010-08-11 17:48:08

0

下面是做到這一點的函數:

function get_array_ranking($array){ 
    $ranking = array(); 
    foreach ($array as $number) { 
    $smaller = 1; 
    foreach ($array as $number2) { 
     if ($number2 < $number) $smaller++; 
    } 
    $ranking[] = $smaller; 
    } 
    return $ranking; 
} 
1

嗯,你可以這樣做:

$b = $a = array(45,3,56,7,21); 
sort($b); 
$r = array_map(
    function ($number) use ($b) { 
     return array_search($number, $b) + 1; 
    }, $a); 

但爲了提高效率,你必須實現你的o排序功能。這裏有一個Java實現我沒有刻意去(從weka)翻譯:

/** 
* Sorts a given array of integers in ascending order and returns an 
* array of integers with the positions of the elements of the original 
* array in the sorted array. The sort is stable. (Equal elements remain 
* in their original order.) 
* 
* @param array this array is not changed by the method! 
* @return an array of integers with the positions in the sorted 
* array. 
*/ 
public static int[] sort(int[] array) { 

    int[] index = new int[array.length]; 
    int[] newIndex = new int[array.length]; 
    int[] helpIndex; 
    int numEqual; 

    for (int i = 0; i < index.length; i++) { 
     index[i] = i; 
    } 
    quickSort(array, index, 0, array.length - 1); 

    // Make sort stable 
    int i = 0; 
    while (i < index.length) { 
     numEqual = 1; 
     for (int j = i + 1; ((j < index.length) 
       && (array[index[i]] == array[index[j]])); 
       j++) { 
      numEqual++; 
     } 
     if (numEqual > 1) { 
      helpIndex = new int[numEqual]; 
      for (int j = 0; j < numEqual; j++) { 
       helpIndex[j] = i + j; 
      } 
      quickSort(index, helpIndex, 0, numEqual - 1); 
      for (int j = 0; j < numEqual; j++) { 
       newIndex[i + j] = index[helpIndex[j]]; 
      } 
      i += numEqual; 
     } else { 
      newIndex[i] = index[i]; 
      i++; 
     } 
    } 
    return newIndex; 
} 

private static void quickSort(int[] array, int[] index, 
     int left, int right) { 

    if (left < right) { 
     int middle = partition(array, index, left, right); 
     quickSort(array, index, left, middle); 
     quickSort(array, index, middle + 1, right); 
    } 
} 
0

,如果您使用排序功能保持原始鍵,如ASORT很簡單:

而且,由於這種使用內部PHP排序,執行時間排序爲n的log(n)我相信

$myArray = (45,3,56,7,21); 
$myArrayCopy = $myArray; 
//asort will sort an array while preserving the key value 
asort($myArrayCopy); 

foreach($myArraycopy as $key => $value) 
{ 
    //map the keys of the array to the original index locations 
    $sortIndexArray[] = $key; 
}