This is probably what you want:
Code
$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>';
What happens here is you create the reward key and fill it with the person's rank based on the index in the $ranks
array. This example is not safe tho. You might want to do a check first if the key even exists in the $ranks
array.
This example is very "raw" and probably isn't flawed with different rankings, so you have to do the final tests yourself.
Output
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
)
)