用日,月和年PHP的阵列组 [英] php array group by using day,month and year

查看:71
本文介绍了用日,月和年PHP的阵列组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有PHP数组是这样的:

I have the php array like this:

Array
(
    [0] => Array
        (            
            [time_stamp] => 1287484988
            [date_time] => Tuesday, 19 October 2010 16:13:7
            [day] => 19
            [month] => 10
            [year] => 2010
            [time_spent] => 41
        )

    [1] => Array
        (         
            [time_stamp] => 1287484662
            [date_time] => Tuesday, 19 October 2010 16:7:41
            [day] => 19
            [month] => 10
            [year] => 2010
            [time_spent] => 215           
        )
)

假设在一月的总天数为:31

Suppose in january the total number of days is: 31

如何通过循环的天数(31)的数量来获得time_spent的SUM使用 GROUP(time_spent每天给定月份)BY TIME_STAMP其中天= $日,月= $一个月,使用PHP =每年$年

How do i loop through the number of days(31) to get the SUM of time_spent(time_spent for every day for a given month) using GROUP BY time_stamp where day=$day,month=$month,year=$year using php

感谢ü。

推荐答案

基本版本是

$sums = array();

foreach ($your_array as $entry) {
   if (!isset($sums[$entry['year']]) {
        $sums[$entry['year']] = array();
   }
   if (!isset($sums[$entry['year']][$entry['month']]) {
        $sums[$entry['year']][$entry['month']] = array();
   }
   if (!isset($sums[$entry['year']][$entry['month']][$entry['day']]) {
        $sums[$entry['year']][$entry['month']][$entry['day']] = 0;
   }

   $sums[$entry['year']][$entry['month']][$entry['day']] += $entry['time_spent'];
}

丑,但3 如果() prevent被吐出的 $款项各种警告阵列建造的。

Ugly, but the 3 if() prevent various warnings from being spit out as the $sums array is built.

这篇关于用日,月和年PHP的阵列组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆