1
occurrence of A----5 times
occurrence of B----7 times
occurrence of C----6 times
($total['A']=5;
 $total['B']=7;
 $total['C']=6;)

我需要根据它们的发生率对每个事件进行如下评分,

$rating['A']=3;
$rating['B']=1;//most occurrence will get rate 1.
$rating['C']=2;
4

2 回答 2

2
asort(&$total);
$rating = 1;
foreach (array_reverse($total) as $key => $val)
    $total[$key] = $rating++;
于 2013-01-24T15:40:21.257 回答
0

您还可以尝试使用数组函数,例如首先对值进行排序并获取排序数组的键,然后使用提取的键作为值创建另一个数组。现在你得到了相同的数组和它们的排名,除了你的排名是“0”,因为数组从索引 0 开始。

可能你可以用 '0' 填充一个附加值,然后最终删除它。

像这样的东西,

$total['A']=5;
$total['B']=7;
$total['C']=6;

$total[0]=0;
asort($total);
$total = array_slice(array_flip(array_keys($total)),1);

或者如果您不喜欢填充额外的值,您可以尝试这种方式。使用键和排序数组的计数创建一个数组。

asort($total);
$total = array_combine(array_keys($total),range(1, count($total)));

这可能是一种快速简便的方法。希望这可以帮助。

于 2013-01-24T16:15:47.567 回答