doushi9729 2019-04-01 10:59
浏览 28
已采纳

如何获取多维数组的最大值30天

I am creating a historical Graph using ChartJS. And I need to Filter the array Values 30 Days Ago

I have an array with the values below:

[0] => Array
    (
        [datetime] => 4/1/2019 4:00:00 PM - 5:00:00 PM
        [value_raw] => 100
    )

[1] => Array
    (
        [datetime] => 4/1/2019 3:00:00 PM - 4:00:00 PM
        [value_raw] => 101
    )

[2] => Array
    (
        [datetime] => 4/1/2019 2:00:00 PM - 3:00:00 PM
        [value_raw] => 99
    )

The Array has large content and contains up to more than 5 months value of every hour every day

This is my current php code as of now for the labels in the ChartJS Script:

LABELS CODE:

labels: [
    <?php
        for($i=0; $i<=30; $i++)
        {
         $label = date('n/d/Y', strtotime('-'.$i.' days',strtotime(date('n/d/Y'))));
         echo '"'.$label.'",';
        }
    ?>
],

Result:

4/01/2019, 3/31/2019, 3/30/2019, 3/29/2019

However I'am having hard time on how am I going to get the equivalent MAX Value per day in parallel to/matching my Labels Code.

DATA CODE:

data: [
    <?php
    foreach ($my_array as $key => $value) {
        for($i=0; $i<=30; $i++)
        {
            $label = date('n/d/Y', strtotime('-'.$i.' days',strtotime(date('n/d/Y'))));

             if ($label == changeFormat($value[datetime]))
             {
                     echo $value['value_raw'].',';
             }
        }
    }
    ?>
],

My Expected Result should be the MAX Value of the Day of the Past 30 Days :

Example:

DATE        VALUE
4/01/2019   99
3/31/2019   101
3/30/2019   100
3/29/2019   95

Result:

99, 101, 100, 95
  • 写回答

4条回答 默认 最新

  • doulingzhuang3079 2019-04-01 12:18
    关注

    As the question was about sorting multi-dimesional array based on the key, try this

    // remove dates older than 30 days
    function remove30DayBeforeDates($a) {
        $pieces = explode(' ', $a['datetime']);
        return strtotime($pieces[0]) > strtotime('-30 days');
    }
    // remove dates older than 30 days
    
    $array = array(
        array(
            'datetime' => '3/29/2019 4:00:00 PM - 5:00:00 PM',
            'value_raw' => 95
        ), array(
            'datetime' => '4/01/2019 3:00:00 PM - 4:00:00 PM',
            'value_raw' => 99
        ), array(
            'datetime' => '3/31/2019 2:00:00 PM - 3:00:00 PM',
            'value_raw' => 101
        ), array(
            'datetime' => '2/10/2019 2:00:00 PM - 3:00:00 PM',
            'value_raw' => 112
        ), array(
            'datetime' => '3/30/2019 2:00:00 PM - 3:00:00 PM',
            'value_raw' => 100
            ));
    
    $array = array_filter($array, "remove30DayBeforeDates");
    
    // sort array based on the date
    
    function check($a, $b) {
        $pieces1 = explode(' ', $a['datetime']);
        $pieces2 = explode(' ', $b['datetime']);
        return strtotime($pieces2[0]) <=> strtotime($pieces1[0]); // use spaceship operator
    }
    
    // sort array based on the date
    
    usort($array, "check");
    
    $value_raw = array_column($array, 'value_raw'); // get array of values from column "value_raw"
    
    print_r(implode(',',$value_raw));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误