PHP计算相等数组值的分数
我有一个值数组,按从高到低的顺序排列 我想为每个数组值奖励点数,这样最低的值得到1分,次低的2分,依此类推。如果两个或多个值相等,我将所有值相加,就好像它们不相等一样,并给每个值一个平均值 下面是一个例子:PHP计算相等数组值的分数,php,arrays,Php,Arrays,我有一个值数组,按从高到低的顺序排列 我想为每个数组值奖励点数,这样最低的值得到1分,次低的2分,依此类推。如果两个或多个值相等,我将所有值相加,就好像它们不相等一样,并给每个值一个平均值 下面是一个例子: Value | Points 59 | 8 56 | 7 55= | 5,5 (would get 6 if it's not a tie) 55= | 5,5 (would get 5 if it's not a tie) 54 |
Value | Points
59 | 8
56 | 7
55= | 5,5 (would get 6 if it's not a tie)
55= | 5,5 (would get 5 if it's not a tie)
54 | 4
52 | 3
46 | 2
19 | 1
如何在PHP中实现这一点
谢谢我不知道您希望如何存储数据或如何显示数据,但以下是您希望实现的示例:
$arrayPoints = array();
$array = array(59,56,55,55,55,55,54,52,52,46,19);
$numElems = count($array);
$j = $numElems;
for ($i=0; $i<$numElems; $i++)
{
$arrayPoints[$array[$i]][] = $j;
$j--;
}
$result = array();
foreach ($arrayPoints as $pointsInfoKey => $pointsInfoVal)
{
$i=0;
$totalPoints = 0;
foreach ($pointsInfoVal as $val)
{
$totalPoints += $val;
$i++;
}
if ($i!=0) $totalPoints = $totalPoints/$i;
echo "Value: $pointsInfoKey - Points: $totalPoints \n";
}
我希望有帮助 根据领带给出所有数值的平均值。 假设55分有三个条目,对应的分数是5、6、7
然后你可以为每个55人提供一个5+6+7/3的点为什么5对一,6对另一个?我在没有领带的时候用简单的for循环计算点,但我不知道当我们有领带时该怎么办,所以我请求你的帮助。
Value: 59 - Points: 11
Value: 56 - Points: 10
Value: 55 - Points: 7.5
Value: 54 - Points: 5
Value: 52 - Points: 3.5
Value: 46 - Points: 2
Value: 19 - Points: 1