PHP中数组值的排名/位置

时间:2014-04-03 08:04:13

标签: php arrays

我有一个包含这些值的数组Array ( [0] => 12 [1] => 17 [2] => 5 [3] => 27 [4] => 5 ),我需要找出每个值的排名/位置 即,

for 27 - Rank 1 
for 17 - Rank 2 
for 12 - Rank 3 
for 5 - Rank 4 
for 5 - Rank 4 (duplicate value in the array) 

并且这些需要与数组的顺序相同,因此输出应如下所示

12 - Rank 3 
17 - Rank 2 
5 - Rank 4 
27 - Rank 1 
5 - Rank 4 

5 个答案:

答案 0 :(得分:2)

这应该有效(包括具有相同等级的重复值):

$values = array();
$values[0] = 5;
$values[1] = 12;
$values[2] = 19;
$values[3] = 9;
$values[4] = 5;

$ordered_values = $values;
rsort($ordered_values);

foreach ($values as $key => $value) {
    foreach ($ordered_values as $ordered_key => $ordered_value) {
        if ($value === $ordered_value) {
            $key = $ordered_key;
            break;
        }
    }
    echo $value . '- Rank: ' . ((int) $key + 1) . '<br/>';
}

答案 1 :(得分:1)

试试这个

$array = Array ( "0" => 12 , "1" => 17 , "2" => 5, "3" => 27, "4" => 5 );

$i=1;
foreach($array as $key=>$values)
{
    $max = max($array);
    echo "\n".$max." rank is ". $i."\n";
    $keys = array_search($max, $array);    
    unset($array[$keys]);
    if(sizeof($array) >0)
    if(!in_array($max,$array))
        $i++;

}

Demo

答案 2 :(得分:0)

您可以按值对数组进行排序:

$ranked = sort($arrayValues);

在此之后,您可以执行for循环,循环遍历$ranked数组,然后按顺序打印数组:

for($i = 0; $i < length($ranked); $i++){
    for($j = 0; $j < length($arrayValues); $j++){
        if($arrayValues[$i] == $sorted[$j]){
            echo $arrayValues[$i] . ' has rank ' . $j;
         }
    }
}

答案 3 :(得分:0)

您遇到的一个问题是,您希望某个返回值具有相同值和密钥的2倍(5 =&gt; rank4)。因此,您不能将数组作为键,也不能将值作为键。在这个例子中,我只是制作了一个二维数组。

<?php

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

// your original input
$original = array(12, 17, 5, 27, 5);

// strip duplicates as they are ranked equally
$rankings = array_unique($original);

// apply some sorting so that the ranks are now given by the keys.
rsort($rankings);

// now just use the origincal array and lookup the rankings for each value
$return = array();
foreach($original as $value)
{
    $rankedValue = array();
    $rankedValue['value'] = $value;
    $rankedValue['rank'] = array_search($value, $rankings) + 1;
    $return[] = $rankedValue;
}

var_dump($return);

答案 4 :(得分:0)

在为自己寻找一个好的解决方案时遇到过这个话题。将它放在这里以备将来使用。我使用的是以下功能:

/* Assign rank to each value of the array $in. 
 * Args:
 *   in (array): Array containing as set of numeric values.
 *
 * Returns:
 *   Returns an array of the same length with ranks. Highest
 *   values of $in get rank 1, lower values get higher ranks.
 *   The same values are attributed to the same ranks.
 */
function array_rank( $in ) {
    # Keep input array "x" and replace values with rank.
    # This preserves the order. Working on a copy called $x
    # to set the ranks.
    print "input\n";
    print_r($in);
    $x = $in; arsort($x); 
    print "sorted\n";
    print_r($x);
    # Initival values
     $rank       = 0; 
    $hiddenrank = 0;
    $hold = null;
    foreach ( $x as $key=>$val ) {
        # Always increade hidden rank
        $hiddenrank += 1;
        # If current value is lower than previous:
        # set new hold, and set rank to hiddenrank.
        if ( is_null($hold) || $val < $hold ) {
            $rank = $hiddenrank; $hold = $val;
        }    
        # Set rank $rank for $in[$key]
        $in[$key] = $rank;
    }  
    print "ranking result\n";
    print_r($in);
    return $in; 
}
$a = array(140,180,180,100);
array_rank( $a ); 

print语句(开发)返回以下内容:

input
Array
(
   [0] => 140
    [1] => 180
    [2] => 180
    [3] => 100
)
sorted
Array
(
    [2] => 180
    [1] => 180
    [0] => 140
    [3] => 100
)
ranking result
Array
(
    [0] => 3
    [1] => 1
    [2] => 1
    [3] => 4
)

如果您想要反向排名(较低的值更好),只需将rasort替换为asort即可。在我的情况下,我是排名积分,越高越好。数组(140,180,180,100)获得排名(3,1,1,4)。具有相同分数的玩家获得相同的排名(在此情况下排名为1),而排名2则被排除在外。