N
N
nepster-web2014-03-14 21:18:02
PHP
nepster-web, 2014-03-14 21:18:02

How to process an array and calculate the necessary data?

I'm scratching my head over this issue.
I have an array like this:

array(
    array('earn'=>'12'),
    array('earn'=>'15'),
    array('earn'=>'10',),
   array('earn'=>'9'),
);

earn is the user's achievements in the game, for example, these are game points. In this case, there are 4 users (although there can be as many as 2 or more) who have finished the game. It is necessary to determine the winners and add 2 more keys.
winner = -1, 1, or 0. User tied, won, or lost
award = 1 to 100. User reward in %
From such an array
array(
    array('earn'=>'12'),
    array('earn'=>'15'),
    array('earn'=>'10',),
   array('earn'=>'9'),
);

Need to get one like this
array(
    array('earn'=>'15', 'winner '=> 1, award => 100),
    array('earn'=>'12', 'winner '=> 0, award => 0),
    array('earn'=>'10', 'winner '=> 0, award => 0),
   array('earn'=>'9, 'winner '=> 0', award => 0),
);

Now here's where the difficulty comes in:
You need to test all the users to find a clear winner. That is, if, for example, a situation arises when users have scored the same number of points, you need to correctly distribute the data.
Examples:
Пример 1, Явный победитель:
 array(
    array('earn'=>'12'),
    array('earn'=>'15'),
    array('earn'=>'10',),
   array('earn'=>'9'),
);
Результат:
 array(
    array('earn'=>'15', 'winner '=> 1, award => 100),
    array('earn'=>'12', 'winner '=> 0, award => 0),
    array('earn'=>'10', 'winner '=> 0, award => 0),
   array('earn'=>'9, 'winner '=> 0', award => 0),
);

----------------------

Пример 2, Спорная победа:
 array(
    array('earn'=>'15'),
    array('earn'=>'15'),
    array('earn'=>'10',),
   array('earn'=>'9'),
);
Результат:
 array(
    array('earn'=>'15', 'winner '=> -1, award => 50),
    array('earn'=>'15, 'winner '=> -1, award => 50),
    array('earn'=>'10', 'winner '=> 0, award => 0),
   array('earn'=>'9, 'winner '=> 0', award => 0),
);

----------------------

Пример 3, Спорная победа 2:
 array(
    array('earn'=>'15'),
    array('earn'=>'15'),
    array('earn'=>'15',),
   array('earn'=>'9'),
);
Результат:
 array(
    array('earn'=>'15', 'winner '=> -1, award => 33.33),
    array('earn'=>'15, 'winner '=> -1, award => 33.33),
    array('earn'=>'15', 'winner '=> -1, award => 33.33),
   array('earn'=>'9, 'winner '=> 0', award => 0),
);

----------------------

Пример 4, Общая ничья:
 array(
    array('earn'=>'15'),
    array('earn'=>'15'),
    array('earn'=>'15',),
   array('earn'=>'15'),
);
Результат:
 array(
    array('earn'=>'15', 'winner '=> -1, award => 25),
    array('earn'=>'15, 'winner '=> -1, award => 25),
    array('earn'=>'15', 'winner '=> -1, award => 25),
   array('earn'=>'15,  'winner '=> -1', award => 25),
);

In general, the whole brain has already been broken by this task, it just doesn’t work out nicely to bypass the array and form the necessary data. Can you please tell me how to organize this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2014-03-14
@nepster-web

What's the problem?

$totalAward = 100;
$maxEarn = -1;
$numWinners = 0;
foreach($gamers as $gamer) {
    if ($gamer['earn'] == $maxEarn)
        $numWinners++;
    elseif ($gamer['earn'] > $maxEarn) {
        $maxEarn = $gamer['earn'];
        $numWinners = 1;
    }
}
$result = array();
foreach($gamers as $gamer) {
    if ($gamer['earn'] == $maxEarn) {
        $gamer['winner'] = -1;
        $gamer['award'] = $totalAward/$numWinners;
    } else {
        $gamer['winner'] = 0;
        $gamer['award'] = 0;
    }
    $result[] = $gamer;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question