根据月份或年份拆分日期数组
我有一个数组,它返回所请求的一系列日期,我想根据变量按月或年将其分解。
当前数组的示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西吗?
Something like this?