I have an array of players and scores, the array looks like:-
$a = array(
array('date' => '09-04-2018','item' => 'player 1', 'score' => '1'),
array('date' => '09-04-2018','item' => 'player 2', 'score' => '2'),
array('date' => '10-04-2018','item' => 'player 1', 'score' => '1'),
array('date' => '10-04-2018','item' => 'player 2', 'score' => '2'),
array('date' => '16-04-2018','item' => 'player 1', 'score' => '3'),
array('date' => '16-04-2018','item' => 'player 2', 'score' => '4'),
array('date' => '17-04-2018','item' => 'player 1', 'score' => '3'),
array('date' => '17-04-2018','item' => 'player 2', 'score' => '4')
);
For simplicity I have only shown 2 players "player 1" and "player 2" in reality there is multiple players
I would like to display a table but have the option to show the score by day, week, or month, I need to put this into an array so that I can pass it to datatables.
So the new array for week would look like:-
$c = array(
array('date' => '15','item' => 'player 1', 'score' => '2'),
array('date' => '15','item' => 'player 2', 'score' => '4'),
array('date' => '16','item' => 'player 1', 'score' => '6'),
array('date' => '16','item' => 'player 2', 'score' => '8')
);
I can add the scores based on dates but this is adding all players and not keeping each player separate.
This is what I have so far:-
<?php
$view = 0;
$a = array(
array('date' => '09-04-2018','item' => 'player 1', 'score' => '1'),
array('date' => '09-04-2018','item' => 'player 2', 'score' => '2'),
array('date' => '10-04-2018','item' => 'player 1', 'score' => '1'),
array('date' => '10-04-2018','item' => 'player 2', 'score' => '2'),
array('date' => '16-04-2018','item' => 'player 1', 'score' => '3'),
array('date' => '16-04-2018','item' => 'player 2', 'score' => '4'),
array('date' => '17-04-2018','item' => 'player 1', 'score' => '3'),
array('date' => '17-04-2018','item' => 'player 2', 'score' => '4')
);
$c =[];
foreach ($a as $i => $b)
{
$date = new DateTime($b['date']);
$player = $b['item'];
$score = $b['score'];
$key = $i;
if ($view == 1) //$view - 0 = day, 1 = week, 2 = month
{
$key = array_search($date, array_column($c, 'date'));
if ($key !== false)
{
$score= $c[$key]['score'] + $score;
}
else
{
$key = count($c);
}
}
$c[$key]['date'] = $date->format("W");
$c[$key]['item'] = $player;
$c[$key]['score'] = $score;
}
echo '<pre>';
print_r($c);
echo '<pre>';
?>