好吧,我已经为这个问题绞尽脑汁一段时间了,谷歌搜索试图找到任何能给我指明正确方向的东西,但无济于事。
所以我有一个排名排行榜数组,像这样:
[1] = array('points' => '99', 'rank' => '1');
[2] = array('points' => '90', 'rank' => '2');
[3] = array('points' => '90', 'rank' => '2');
[4] = array('points' => '80', 'rank' => '4');
[5] = array('points' => '70', 'rank' => '5');
[6] = array('points' => '70', 'rank' => '5');
然后我有一个奖励积分系统,其中包含一个预定义的数组,以根据排名奖励积分:
[1]=10;
[2]=9;
[3]=8;
[4]=7;
[5]=6;
[6]=5;
如果没有关系,我就会匹配两个数组,并根据数组键来奖励积分。但是,因为我允许绑定,所以我尝试在所有绑定的用户之间分配绑定的奖励点。所以根据上面的参考,并列第二名的玩家2和3将分享第9点和第8点的奖励……所以两人都得8.5分。
所以当合并两个数组时,我要寻找的最终结果是:
[1] = array('points' => '99', 'rank' => '1', 'reward' => '10');
[2] = array('points' => '90', 'rank' => '2', 'reward' => '8.5');
[3] = array('points' => '90', 'rank' => '2', 'reward' => '8.5');
[4] = array('points' => '80', 'rank' => '4', 'reward' => '7');
[5] = array('points' => '70', 'rank' => '5', 'reward' => '5.5');
[6] = array('points' => '70', 'rank' => '5', 'reward' => '5.5');
排行榜可以不断变化,直到一天结束,所以我尽量不要把它弄得太复杂。我只是想知道有没有人能给我指出一些他们看到的对我有帮助的东西或者他们是否有任何实现的想法。
在我的尝试中,我最挣扎的部分是循环遍历排行榜数组,并不断期待"排名"相同,然后知道我需要总结和分割的内容,同时也知道我可以跳过数组,而不是在我刚刚经历的下一个项目上执行相同的测试。对不起,如果我弄糊涂了,但我显然是糊涂了。
这可能就是你想要的:
$persons = array(
array('points' => '99', 'rank' => '1'),
array('points' => '90', 'rank' => '2'),
array('points' => '90', 'rank' => '2'),
array('points' => '80', 'rank' => '4'),
array('points' => '70', 'rank' => '5'),
array('points' => '70', 'rank' => '5')
);
$ranks = array(
1 => 10,
2 => 9,
3 => 8,
4 => 7,
5 => 6,
6 => 5
);
foreach($persons as $person => $prop) {
$reward = $ranks[$prop['rank']];
if (isset($persons[$person+1])) {
if ($persons[$person+1]['rank'] == $prop['rank']) {
$reward = $reward - 0.5;
}
}
if (isset($persons[$person-1])) {
if ($persons[$person-1]['rank'] == $prop['rank']) {
$reward = $reward - 0.5;
}
}
$persons[$person]['reward'] = $reward;
}
echo '<pre>';
print_r($persons);
echo '<pre>';
这里发生的是,您创建奖励键并根据$ranks
数组中的索引填充人员的排名。但是这个例子并不安全。您可能需要先检查键是否存在于$ranks
数组中。
这个例子非常"原始",可能没有不同排名的缺陷,所以你必须自己做最终测试。
Array
(
[0] => Array
(
[points] => 99
[rank] => 1
[reward] => 10
)
[1] => Array
(
[points] => 90
[rank] => 2
[reward] => 8.5
)
[2] => Array
(
[points] => 90
[rank] => 2
[reward] => 8.5
)
[3] => Array
(
[points] => 80
[rank] => 4
[reward] => 7
)
[4] => Array
(
[points] => 70
[rank] => 5
[reward] => 5.5
)
[5] => Array
(
[points] => 70
[rank] => 5
[reward] => 5.5
)
)
如果超过2个人可以有相同的排名
$persons = array(
array('points' => '99', 'rank' => '1'),
array('points' => '90', 'rank' => '2'), // <---
array('points' => '90', 'rank' => '2'), // <---
array('points' => '90', 'rank' => '2'), // <---
array('points' => '70', 'rank' => '5'),
array('points' => '70', 'rank' => '5')
);
那么这个@Allendar算法会给你错误的结果(会给8.5比8分的奖励,给上面输入中排名2的人8.5分)。
如果总是有相同的人数和定义的排名(如6在这种情况下),你可以使用这样的东西:
$persons = array(
array('points' => '99', 'rank' => '1'),
array('points' => '90', 'rank' => '2'),
array('points' => '90', 'rank' => '2'),
array('points' => '90', 'rank' => '2'),
array('points' => '70', 'rank' => '5'),
array('points' => '70', 'rank' => '5')
);
$ranks = array(
1 => 10,
2 => 9,
3 => 8,
4 => 7,
5 => 6,
6 => 5
);
$current_ranks = $ranks;
$rank_pool = array();
$rank_counter = array();
$i = 1; // person counter, start with 1 because $ranks start with 1
// distribute reword equally for all ranks
foreach($persons as $person => $prop) {
if(!isset($ranks[$i])){
exit("no reword for this much persons [$i]");
}
$rank = $prop['rank'];
if(!isset($rank_pool[$rank])){
$rank_pool[$rank] = 0;
}
$rank_pool[$rank] += $ranks[$i];
if(!isset($rank_counter[$rank])){
$rank_counter[$rank] = 0;
}
$rank_counter[$rank]++;
$i++;
}
// set reword according to equally distributed reword
// points for all ranks
foreach($persons as $person => $prop) {
$rank = $prop['rank'];
if(!isset($rank_pool[$rank]) || !isset($rank_counter[$rank])){
exit('something is wrong!');
}
$persons[$person]['reword'] = $rank_pool[$rank]/$rank_counter[$rank];
}
echo '<pre>';
print_r($rank_counter);
print_r($rank_pool);
print_r($persons);
echo '<pre>';
和上面的输入,你会得到:
$rank_counter=
Array
(
[1] => 1// --- 1 with rank 1
[2] => 3// --- 3 with rank 2
[5] => 2// --- 2 with rank 5
)
$rank_pool
Array
(
[1] => 10// --- 10 points for persons with rank 1
[2] => 24// --- 24 points for persons with rank 2
[5] => 11// --- 11 points for persons with rank 5
)
,你可以通过简单地划分每个等级的总分数和每个等级的计数得到每个人的奖励分数
10/1 = 10 points for each person with rank 1
24/3 = 8 points for each person with rank 2
and
11/2 = 5.5 points for each person with rank 5