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;
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;
asort(&$total);
$rating = 1;
foreach (array_reverse($total) as $key => $val)
$total[$key] = $rating++;
干净,漂亮! – perrohunter
谢谢@Cobra_Fast –
你也可以尝试使用像第一个排序值的阵列功能,并获得钥匙然后使用提取的键作为值创建另一个数组。现在你可以得到与他们的排名相同的数组,除了当排列以索引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)));
这可能是一种快速简便的方法。希望这可以帮助。
当两个总数相等时会发生什么? – cdhowie
@OneTrickPony代码现在是正确的形式。:-) –