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

如何获取多维数组的最大值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
    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));
    
    点赞 评论
  • dongsi4815
    dongsi4815 2019-04-01 11:24
        $today= date('Y-m-d');
        $thirty_days_ago = date('Y-m-d', strtotime('-30 days'));
    

    then, in db query builder,

        $data = DB::table('your_table_name')
               ->where('datetime','>='$thirty_days_ago)
               ->where('datetime','<='$today)
               ->whereRaw('value_raw = (select max(`value_raw`) from your_table_name)')
               ->select('datetime','value_raw')
               ->groupBy('datetime')->get();
    
    点赞 评论
  • dousong1926
    dousong1926 2019-04-01 11:27

    You can fire query if you are using mysql to group by date and get max value,

    SELECT MAX(value_raw), DATE(mydate) DateOnly FROM a_table where mydate BETWEEN (NOW() - INTERVAL 30 DAY) AND NOW() GROUP BY DateOnly;
    
    点赞 评论
  • dou47278
    dou47278 2019-04-02 02:11

    Hi guys this is what i have come up with:

    data: [
    <?php
      for($i=0; $i<=7; $i++)
      {
          $date = date('n/j/Y', strtotime('-'.$i.' days',strtotime(date('n/d/Y'))));
    
          $filterBy     = $date;
          $new_array    = array_filter($my_array, function ($array) use ($filterBy) {
              $datetime = date("n/j/Y",changeFormat($array['datetime']));
              if ($datetime == $filterBy)
              {
                return $datetime;
              }
          });
    
          if (count($new_array) != 0)
          {
            $max_value = max(array_column($new_array, 'value_raw'));
            echo number_format($max_value, 0, '.', '').",";
          }
      }
    ?> ],
    
    点赞 评论

相关推荐