必须选择To-date 和from-date。我有一个数组,其键为日期我想要的是,如果我选择 15 天的键值,最多 15 的键值将添加到一列,所以如果我的迄今为止和起始日期的差异为 30,那么它将出现两列 1 -15 / 16-30 并且值将被添加并根据
Array
(
[2018-08-01] => Array
(
[male] => 3
[female] => 0
)
[2018-08-02] => Array
(
[male] => 1
[female] => 0
)
[2018-08-06] => Array
(
[male] => 6
[female] => 2
)
[2018-08-11] => Array
(
[male] => 1
[female] => 1
)
[2018-08-17] => Array
(
[male] => 3
[female] => 2
)
[2018-08-18] => Array
(
[male] => 3
[female] => 3
)
[2018-08-03] => Array
(
[male] => 2
[female] => 0
)
[2018-08-04] => Array
(
[male] => 4
[female] => 3
)
)
1-15 dates will be get sum values and 16-30 will get another sum value so how many month will be there like that it will be come 1-15/16-30 manner
This answer is perfectly working
$partitions = 2;
$s_day = 1;
$intervals = [];
while (end($intervals)['to'] != 31) {
$e_tmp = $s_day + (int)(31/$partitions) - 1;
$e_day = $e_tmp > 31 ? 31 : $e_tmp;
$intervals[] = ['from' => $s_day, 'to' => $e_day];
$s_day = $e_day + 1;
}
// partition info by intervals
$info_partitioned = [];
foreach ($generesult as $date => $info) {
// parse month and day
preg_match('/(\d+-\d+)-(\d+)/', $date, $matches);
// choose interval
$found = null;
foreach ($intervals as $days) {
if ($matches[2] >= $days['from'] && $matches[2] <= $days['to']) {
$found = ' : ' . $days['from'] . '-' . $days['to'];
break;
}
}
// put info into required partition
$info_partitioned[$matches[1] . $found][] = $info;
}
// sum males/females in each partition
$results = [];
foreach ($info_partitioned as $partition => $counts) {
foreach($counts as $stat) {
$results[$partition]['male'] += $stat['male'];
$results[$partition]['female'] += $stat['female'];
}
}
print_r($results);