如何为数组分配排名

时间:2010-08-11 17:38:52

标签: php

我有一组数字,例如,myarray =(45,3,56,7,21)。我需要做的是将值排列到另一个数组中,所以对于上面我最终会得到myarray2 =(4,1,5,2,3)。

非常感谢,

亚当

6 个答案:

答案 0 :(得分: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 )
*/
?>

答案 1 :(得分: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);
}

答案 2 :(得分: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);

但为了提高效率,您必须实现自己的排序功能。这是一个我无法翻译的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);
    }
}

答案 3 :(得分:0)

获取一些数组,计算小于该数组的数字,添加一个数字,然后获得排名。

答案 4 :(得分: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;
}

答案 5 :(得分: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;
}