按列值的前导字符对数据行进行分组
我有一个如下所示的数组,我想要做的是根据 timestamp
列的日期子字符串创建多个子数组,例如 (2011-02-04
)
Array
(
[0] => Array
(
[avgvalue] => 0
[maxvalue] => 0
[minvalue] => 0
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T11:00:00.000Z
)
[1] => Array
(
[avgvalue] => 268.3
[maxvalue] => 268.3
[minvalue] => 268.3
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T12:00:00.000Z
)
[2] => Array
(
[avgvalue] => 268.391666667
[maxvalue] => 268.4
[minvalue] => 268.3
[nrsamples] => 0.0288675134595
[stddeviation] => 0.0288675134595
[timestamp] => 2011-02-04T13:00:00.000Z
)
[3] => Array
(
[avgvalue] => 268.433333333
[maxvalue] => 268.5
[minvalue] => 268.4
[nrsamples] => 0.0492365963918
[stddeviation] => 0.0492365963918
[timestamp] => 2011-02-04T14:00:00.000Z
)
[4] => Array
(
[avgvalue] => 268.5
[maxvalue] => 268.5
[minvalue] => 268.5
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T15:00:00.000Z
)
[5] => Array
(
[avgvalue] => 268.575
[maxvalue] => 268.6
[minvalue] => 268.5
[nrsamples] => 0.0452267016867
[stddeviation] => 0.0452267016867
[timestamp] => 2011-02-04T16:00:00.000Z
)
[6] => Array
(
[avgvalue] => 268.616666667
[maxvalue] => 268.7
[minvalue] => 268.6
[nrsamples] => 0.0389249472081
[stddeviation] => 0.0389249472081
[timestamp] => 2011-02-04T17:00:00.000Z
)
[7] => Array
(
[avgvalue] => 268.7
[maxvalue] => 268.7
[minvalue] => 268.7
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T18:00:00.000Z
)
[8] => Array
(
[avgvalue] => 268.741666667
[maxvalue] => 268.8
[minvalue] => 268.7
[nrsamples] => 0.0514928650545
[stddeviation] => 0.0514928650545
[timestamp] => 2011-02-04T19:00:00.000Z
)
[9] => Array
(
[avgvalue] => 268.8
[maxvalue] => 268.8
[minvalue] => 268.8
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T20:00:00.000Z
)
[10] => Array
(
[avgvalue] => 268.883333333
[maxvalue] => 268.9
[minvalue] => 268.8
[nrsamples] => 0.0389249472081
[stddeviation] => 0.0389249472081
[timestamp] => 2011-02-04T21:00:00.000Z
)
)
:上面的数组在每个子数组中都有一个时间戳键。我分解了时间戳值以将日期与时间分开,现在我无法将数组拆分为子数组。
我想要的是有一个 2011-02-04
数组(包含该日期的所有值)和另一个 2011-02-05
数组(包含所有那个日期)。这可以是动态的,我的意思是日期可以更多。那么,我该怎么做呢?
我希望它是:
array[0] => array(... list of all the values for 2011-02-04),
array[1] => array(...list of all values for 2011-02-05)
I have an array as shown below, what I wanted to do is create multiple subarrays based on the date substring of the timestamp
column like (2011-02-04
):
Array
(
[0] => Array
(
[avgvalue] => 0
[maxvalue] => 0
[minvalue] => 0
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T11:00:00.000Z
)
[1] => Array
(
[avgvalue] => 268.3
[maxvalue] => 268.3
[minvalue] => 268.3
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T12:00:00.000Z
)
[2] => Array
(
[avgvalue] => 268.391666667
[maxvalue] => 268.4
[minvalue] => 268.3
[nrsamples] => 0.0288675134595
[stddeviation] => 0.0288675134595
[timestamp] => 2011-02-04T13:00:00.000Z
)
[3] => Array
(
[avgvalue] => 268.433333333
[maxvalue] => 268.5
[minvalue] => 268.4
[nrsamples] => 0.0492365963918
[stddeviation] => 0.0492365963918
[timestamp] => 2011-02-04T14:00:00.000Z
)
[4] => Array
(
[avgvalue] => 268.5
[maxvalue] => 268.5
[minvalue] => 268.5
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T15:00:00.000Z
)
[5] => Array
(
[avgvalue] => 268.575
[maxvalue] => 268.6
[minvalue] => 268.5
[nrsamples] => 0.0452267016867
[stddeviation] => 0.0452267016867
[timestamp] => 2011-02-04T16:00:00.000Z
)
[6] => Array
(
[avgvalue] => 268.616666667
[maxvalue] => 268.7
[minvalue] => 268.6
[nrsamples] => 0.0389249472081
[stddeviation] => 0.0389249472081
[timestamp] => 2011-02-04T17:00:00.000Z
)
[7] => Array
(
[avgvalue] => 268.7
[maxvalue] => 268.7
[minvalue] => 268.7
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T18:00:00.000Z
)
[8] => Array
(
[avgvalue] => 268.741666667
[maxvalue] => 268.8
[minvalue] => 268.7
[nrsamples] => 0.0514928650545
[stddeviation] => 0.0514928650545
[timestamp] => 2011-02-04T19:00:00.000Z
)
[9] => Array
(
[avgvalue] => 268.8
[maxvalue] => 268.8
[minvalue] => 268.8
[nrsamples] => 0
[stddeviation] => 0
[timestamp] => 2011-02-04T20:00:00.000Z
)
[10] => Array
(
[avgvalue] => 268.883333333
[maxvalue] => 268.9
[minvalue] => 268.8
[nrsamples] => 0.0389249472081
[stddeviation] => 0.0389249472081
[timestamp] => 2011-02-04T21:00:00.000Z
)
)
The above array has a timestamp key in each subarray. I exploded the timestamp value to separate the date from the time, now I am having trouble splitting the array into sub-arrays.
What I wanted was to have one array for 2011-02-04
(containing all the values for that date) and another array for 2011-02-05
(containing values for all that date). This can be dynamic, I mean the dates can be even more. So, how can I do that?
I want it as:
array[0] => array(... list of all the values for 2011-02-04),
array[1] => array(...list of all values for 2011-02-05)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设所有条目的日期格式都相同(看起来是这样),您可以简单地循环数组:
参考:
strstr
,array_key_exists
根据原始数组中项目的顺序,您可能必须使用
ksort
按时间顺序对$result
数组进行排序。Assuming the date format is the same for all entries (which it appears to be), you can simply loop over the array:
Reference:
strstr
,array_key_exists
Depending on the order of the items in your original array, you might have to use
ksort
to sort the$result
array chronologically.在推送新子数组之前无需检查数组键是否存在。只需将子数组推入日期键控子数组即可。
解决方案很简单:(Demo)
分组后,如果要对组进行排序,请使用
ksort($result)
用于 ASC 或krsort($result)
用于 DESC。如果您想重新索引第一级键,请使用$result = array_values($result)
。There is no need to check if an array key exists before pushing a new subarray. Just push the subarray into the date-keyed subarray.
The solution is as simple as: (Demo)
After grouping, if you want to sort the groups, use
ksort($result)
for ASC orkrsort($result)
for DESC. If you want to reindex the first level keys, use$result = array_values($result)
.