【问题标题】:Group php array by date按日期分组php数组
【发布时间】:2013-09-25 05:56:25
【问题描述】:

我有一个数组:

Array
(
    [0] => Array
        (
            [id] => 81
            [placed] => 2013-09-19 16:32:53
            [sub_total] => 786
        )

    [1] => Array
        (
            [id] => 80
            [placed] => 2013-09-19 16:32:06
            [sub_total] => 780
        )

    [2] => Array
        (
            [id] => 79
            [placed] => 2013-09-18 17:06:48
            [sub_total] => 786
        )

    [3] => Array
        (
            [id] => 78            
            [placed] => 2013-09-18 17:05:02
            [sub_total] => 756
        )

    [4] => Array
        (
            [id] => 77          
            [placed] => 2013-09-17 17:02:53
            [sub_total] => 786
        )

    [5] => Array
        (
            [id] => 76
            [placed] => 2013-09-16 17:02:53
            [sub_total] => 756
        )
)

是否可以按日期对这些数据进行分组并汇总小计以获取输出数组:

 Array
    (
        [0] => Array
            (
                [placed] => 2013-09-19
                [sub_total] => 786 + 780
            )

        [2] => Array
            (
                [placed] => 2013-09-18
                [sub_total] => 786 + 756
            )

        [3] => Array
            (
                [placed] => 2013-09-17 17:02:53
                [sub_total] => 786
            )

        [4] => Array
            (
                [placed] => 2013-09-16 17:02:53
                [sub_total] => 756
            )
    )

【问题讨论】:

  • 如果它的结果​​来自数据库,为什么不从查询中做呢?

标签: php arrays datetime grouping


【解决方案1】:
$output=array();
foreach($yourArray as $values)
{
 $d=date("Y-m-d",strtotime($values["placed"]));
 $output[$d]["sub_total"]+=$values["sub_total"];
}

print_r($output);

Fiddle

致谢:这个小提琴上使用的初始数组取自下面 Jason OOO 的答案。

【讨论】:

  • 错误:未定义索引:2013-09-19
  • 这是一个通知,而不是错误。如果您愿意,您可以先定义该索引
【解决方案2】:

我也对此进行了测试:http://phpfiddle.org/main/code/rzv-ngp

<?php
Array
(
    '0' => Array
        (
            'id' => '81',
            'placed' => '2013-09-19 16:32:53',
            'sub_total' => '786'
        ),

    '1' => Array
        (
            'id' => '80',
            'placed' => '2013-09-19 16:32:06',
            'sub_total' => '780'
        ),

//...
);

$newarray = array();
foreach ($array as $value){
 $temp = explode(" ", $value['placed']);
 $date = $temp[0];
    $total = (isset($newarray[$date]['sub_total']) ? $newarray[$date]['sub_total'] + $value['sub_total']: $value['sub_total']);
 $newarray[$date] = array('placed' => $date, 'sub_total' => $total);
}

print_r($newarray);

?>

【讨论】:

  • 这返回给我: Array ( [2013-09-19] => Array ( [placed] => 2013-09-19 [total_price] => 1850.6
  • 是否可以得到这样的输出:Array ( [0] => Array ( [placed] => 2013-09-19 [total_price] => 1850.6
  • 是的,print_r(array_values($newarray)); 所以答案是使用$newarray = array_values($newarray);
【解决方案3】:

您可以在查询时使用这种数组。像

select date_field_name,other_field from table_name group by Day(date_feild_name);

之后,您可以使用 foreach 来处理每天的数据!

【讨论】:

    【解决方案4】:

    尝试使用子查询

         select field_name,DAY(date_field) as date,(select sum(sub_total) where DAY(date_field)=date) as sub_total from table_name group by day(date_field)
    

    【讨论】:

      【解决方案5】:
      Try this code
      <?php
      $shop = array(array( id => '81', 
                            placed => '2013-09-19',
                            sub_total => '786' 
                          ),
                   array( id =>'80', 
                            placed => '2013-09-19',
                            sub_total => '780',
                          ),
                    array( id => '79', 
                            placed => '2013-09-18',
                            sub_total => '786' 
                          ),
                    array
                              (
                          id => '78',            
                          placed => '2013-09-18',
                          sub_total => '756'
                       ),
                  array(
                           id => '77',          
                          placed => '2013-09-17',
                          sub_total => '786'
                  ),
                  array(
                           id => '76',          
                          placed => '2013-09-16',
                          sub_total => '756'
                  )
                   );
      $result=array();
      foreach($shop as $value)
      {
          if(!isset($result[$value['placed']]))
          {
              //echo $value['placed'];
              //echo $result[$value['placed']];
              $result[$value['placed']]=array('placed'=>$value['placed'],'sub_total'=>0);
      
      
          }
          $result[$value['placed']]['sub_total']+=$value['sub_total'];
      }
      
      print_r($result);
      //print_r($shop);
      ?>
      

      【讨论】:

        猜你喜欢
        • 2020-11-19
        • 1970-01-01
        • 2011-03-23
        • 1970-01-01
        • 1970-01-01
        • 2016-10-03
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多