分手了基于月份或年份日期的数组 [英] Split up Array of Dates based on Month or Year

查看:123
本文介绍了分手了基于月份或年份日期的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回请求一系列的日期,我想无论是通过每月或每年基于变量掰开的Array。

I have an Array that returns a requested series of dates that I would like to break apart by either Month or Year based on a variable.

当前数组的例子:

array(4) {

[0]=> string(10) "2012-02-01"

[1]=> string(10) "2012-02-02"

[2]=> string(10) "2011-02-03"

[3]=> string(10) "2011-03-04"

[4]=> string(10) "2011-04-05"

}

为了产生在月或年视图中的谷歌的图表,我需要拆开打破这种阵列组合在一起基于一个用户preference这将是一场以年或月变量。

In order to generate a google chart in "Month" or "Year" view I need to break this array apart and group together the "Years" or "Months" based on a users preference which will be in a variable.

中我需要什么如果年视图

Example of what I need returned if in "Year View"

array(2) {

 [0]=> Array(2) {

       [0]=> string(10) "2012-02-01"

       [1]=> string(10) "2012-02-02"

}

 [1]=> Array(3) {

       [0]=> string(10) "2011-02-03"

       [1]=> string(10) "2011-03-14"

       [2]=> string(10) "2011-04-18"

} 

}

不是100%肯定,如果这是一个健全的数组,但我需要能够做到为每个阵列设定日期的的foreach语句,以便我能加起来的所有值(所有我需要的是日期功能去获得的价值),并返回1总值与去年这都将是相同的,因为他们分组,我就在爆炸foreach语句最后一个数组的一年。

Not 100% sure if that is a sound array, but I need to be able to do a "foreach" statement for each array set of dates so I can add up all the values (all I need is date for function to go get the value) and return one total value and the year which will all be the same since they are grouped and I'll just explode the year of the last array in the foreach statement.

下面是相同的预期结果,但在月视图模式

Here is the same expected result but in "Month" view mode

array(3) {

 [0]=> Array(3) {

       [0]=> string(10) "2012-02-01"

       [1]=> string(10) "2012-02-02"

       [2]=> string(10) "2011-02-03"

}

 [1]=> Array(1) {



       [1]=> string(10) "2011-03-14"

} 

 [2]=> Array(1) {

       [1]=> string(10) "2011-04-18"

} 

}

任何帮助将大大AP preciated!我似乎无法找到解决的办法,同时保持完好的一切

Any help would be greatly appreciated! I just can't seem to find a solution while keeping everything intact.

推荐答案

这样呢?

$years = Array();
$months = Array();
foreach($dates as $d) {
    list($y,$m) = explode("-",$d);
    $years[$y][] = $d;
    $months[$y."-".$m][] = $d;
}
$years = array_values($years);
$months = array_values($months);

var_dump($years,$months);

这篇关于分手了基于月份或年份日期的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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