显示周或月的项目总计

问题描述:

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>';

 ?>

I think this will help you out.
This will create a new array with player -> week -> scores that can easily be sumed or echoed "as it is".

//Week example

Foreach($arr as $val){
    $res[$val['item']][date("W", strtotime($val['date']))][] = $val['score'];
}
Var_dump($res);

https://3v4l.org/V9Dk9

By changing the date function you can make it build the array differently.
Say you want month instead, change the "W" to n, m, M or F.
https://3v4l.org/MUjYV

Edit:
This should output the sum.

Foreach($res as $player => $time){
    Foreach($time as $timeperiod => $score){
        Echo $player. " ". $timeperiod ." " .  array_sum($score) ."
";
    }
}

Month
https://3v4l.org/2h30B

Weeks
https://3v4l.org/2NAqX

Same code just the date character is different.