Answer the question
In order to leave comments, you need to log in
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'),
);
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),
);
Пример 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),
);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question