dtmjl4427 2018-04-20 13:55
浏览 24
已采纳

显示周或月的项目总计

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

 ?>
  • 写回答

1条回答 默认 最新

  • dongyang5716 2018-04-20 14:11
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch