根据月份或年份拆分日期数组

发布于 2025-01-04 15:07:45 字数 1204 浏览 1 评论 0原文

我有一个数组,它返回所请求的一系列日期,我想根据变量按月或年将其分解。

当前数组的示例:

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"

}

为了在“月”或“年”视图中生成谷歌图表,我需要将此数组分开,并根据用户首选项将“年”或“月”分组在一起,该首选项将位于变量中。

我需要返回的示例,如果在“年视图”中,

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”语句,以便我可以将所有值相加(我需要的只是函数获取值的日期)并返回一个总值和年份,因为它们被分组,所以它们都是相同的,我只会分解 foreach 语句中最后一个数组的年份。

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

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"

} 

}

任何帮助将不胜感激!我似乎无法在保持一切完好无损的情况下找到解决方案。

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.

Example of current array:

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"

}

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"

} 

}

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"

} 

}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2025-01-11 15:07:45

像这样的东西吗?

$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);

Something like this?

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文