Reputation: 23
I have 2 arrays from a results table, Player
and Score
. Each player can submit multiple scores. I want to end up with two arrays, unique players and their best score. I think it should be easy but I cannot work it out.
<?php
$player = array("Alan", "Bill", "Colin", "Dave", "Bill", "Bill", "Colin", "Colin", "Dave");
$playerLen = count($player);
$score = array(1,2,3,4,5,6,7,8,9);
$scoreLen = count($score);
$uniqPlayerAr = array();
$highScoreAr = array();
$i=0;
for ($i = 0; $i <$playerLen; $i++) {
$highScore=0;
$playerName = $player[$i];
$thisScore = $score[$i];
if($thisScore>$highScore){
$highScore = $thisScore;
}
if(in_array($playerName, $uniqPlayerAr, true)){
array_push($highScoreAr, $highScore);
}else{
array_push($uniqPlayerAr, $playerName);
array_push($highScoreAr, $highScore);
}
}
$uniqPlayerArLen = count($uniqPlayerAr);
echo "<br>there are ".$uniqPlayerArLen." uniq Players and ".$scoreLen." scores<br>";
$i = 0;
for ($i = 0; $i <$uniqPlayerArLen; $i++) {
echo "highScore for ".$uniqPlayerAr[$i]." = ".$highScoreAr[$i]."<br>";
}
?>
Upvotes: 0
Views: 73
Reputation: 1702
First Approach
$players = array("Alan", "Bill", "Colin", "Dave", "Bill", "Bill", "Colin", "Colin", "Dave");
$scores = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$players_unique = array_unique($players); // Filter out only the unique values
$players_best_score = [];
foreach ($players_unique as $key => $player_unique) {
$players_score = [];
foreach ($players as $plyr_key => $player) {
if ($player_unique === $player) {
$players_score[] = $scores[$plyr_key]; // Adding all the scores of each player to sum the values at the end of each iteration with `max` function
}
}
$players_best_score[] = max($players_score); // It returns the maximum value in the array so that it's easier to extract the best score of a player from the array.
}
var_dump($players_unique);
var_dump($players_best_score);
OUTPUT
Second Approach
$players = array("Alan", "Bill", "Colin", "Dave", "Bill", "Bill", "Colin", "Colin", "Dave");
$scores = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$players_unique = array_unique($players);
$players_final = [];
foreach ($players_unique as $key => $player_unique) {
$players_score = [];
foreach ($players as $plyr_key => $player) {
if ($player_unique === $player) {
$players_score[] = $scores[$plyr_key];
}
}
$players_final[] = [
'name' => $player_unique,
'best_score' => max($players_score),
'total_score' => array_sum($players_score) // It returns the cumulative value of the array meaning it adds up all the numeric values in the array.
];
}
var_dump($players_final);
OUTPUT
I've added comments to few things in the code to ease up the understanding.
First approach produces two arrays containing name of the players without duplication and best scores of corresponding players respectively.
Second approach produces a single array containing a sub array of each player which contains name, best score and total score. Total score in the sub array is just a added feature.
Hope it helps!
Upvotes: 0
Reputation: 1540
<?php
$player = array("Alan", "Bill", "Colin", "Dave", "Bill", "Bill", "Colin", "Colin", "Dave");
$score = array(1,2,3,4,5,6,7,8,9);
$players= array();
$scores = array();
$highscores=array();
for($i=0 ; $i<=(count($player)-1);$i++){
if(array_search($player[$i],$players)){
$j = array_search($player[$i],$players);
$scores[$j] .='-'.$score[$i];
} else {
array_push($players,$player[$i]);
array_push($scores,$score[$i]);
}
}
foreach ($scores as $newscore){
$newscore= explode('-',$newscore);
array_push($highscores,max($newscore));
}
var_dump($players);
var_dump($highscores);
//Output is $players and $highscores
?>
thank you for improved me
Upvotes: 0
Reputation: 6217
The following code will separate each (high-)score for the users. Run it.
$result = array();
foreach($player as $k => $v){
if(!isset($result[$v])){
$result[$v] = array('scores' => array(), 'high' => null);
}
if($score[$k] > $result[$v]['high']){
$result[$v]['high'] = $score[$k];
}
$result[$v]['scores'][] = $score[$k];
}
To access, eg, Colin's highscore:
echo $result['Colin']['high']; // 8
This is the content of $result
:
Array
(
[Alan] => Array
(
[scores] => Array
(
[0] => 1
)
[high] => 1
)
[Bill] => Array
(
[scores] => Array
(
[0] => 2
[1] => 5
[2] => 6
)
[high] => 6
)
[Colin] => Array
(
[scores] => Array
(
[0] => 3
[1] => 7
[2] => 8
)
[high] => 8
)
[Dave] => Array
(
[scores] => Array
(
[0] => 4
[1] => 9
)
[high] => 9
)
)
Upvotes: 1
Reputation: 6311
loop through both array and push the greater value into the new array
try this
$player = array("Alan", "Bill", "Colin", "Dave", "Bill", "Bill", "Colin", "Colin", "Dave");
$score = array(1,2,3,4,5,6,7,8,9);
foreach($player as $k => $v){
$new[$v] = (!isset($new[$v])) ? $score[$k] : ($score[$k] > $new[$v] ) ? $score[$k] : $new[$v];
}
print_r($new);
Upvotes: 1
Reputation: 318
I would do it like this:
<?php
$player = array("Alan", "Bill", "Colin", "Dave", "Bill", "Bill", "Colin", "Colin", "Dave");
$playerLen = count($player);
$score = array(1,2,3,4,5,6,7,8,9);
$scoreLen = count($score);
$uniqPlayerAr = array();
$highScoreAr = array();
$i=0;
for ($i = 0; $i <$playerLen; $i++) {
$playerName = $player[$i];
if(!isset($highScoreAr[$playerName]) || $highScoreAr[$playerName] < $score[$i]){
$highScoreAr[$playerName] = $score[$i];
}
}
$uniqPlayerArLen = count($highScoreAr);
echo "<br>there are ".$uniqPlayerArLen." uniq Players and ".$scoreLen." scores<br>";
foreach($highScoreAr as $player => $score){
$score;
echo "highScore for " . $player . " = " . $score . "<br>";
}
?>
you can also us the array_unique function, to eliminate duplicates, but that way, you cannot check the highest score per player
Upvotes: 1
Reputation: 420
I think the easiest way would be to create an aux array to hold the players scores. In this array the keys are the player names, and the values the scores.
You travel through the player array and get the player name, then get the score for that player, and check if the score of the player in the aux array is lower than the score; if it is, set the new score.
Maybe the code will be easier to understand:
<?php
$player = array("Alan", "Bill", "Colin", "Dave", "Bill", "Bill", "Colin", "Colin", "Dave");
$score = array(1,2,3,4,5,6,7,8,9);
$finalScores = array();
for ($x = 0, $max = count($player); $x < $max; ++$x)
{
$playerName = $player[$x];
$playerScore = $score[$x];
if ($finalScores[$playerName] < $playerScore)
$finalScores[$playerName] = $playerScore;
}
echo '<pre>';print_r($finalScores);echo '</pre>';
After the loop the $finalScores array will be:
Array
(
[Alan] => 1
[Bill] => 6
[Colin] => 8
[Dave] => 9
)
Upvotes: 1