多维数组根据键值求和并放入单列
multidimensional array sum and put into single column depending on key value
必须选择截止日期和起始日期。
我有一个包含日期键的数组我想要的是如果我 select 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);
sample outputs
如果我对你的理解是正确的——你想将性别信息分成精确月份的相等分区,然后将这些分区中的性别计数相加。这是我想出的代码:
// gender data
$data = [
'2018-08-01' => ['male' => 3, 'female' => 0] ,
'2018-08-02' => ['male' => 1, 'female' => 0] ,
'2018-08-06' => ['male' => 6, 'female' => 2] ,
'2018-08-11' => ['male' => 1, 'female' => 1] ,
'2018-08-17' => ['male' => 3, 'female' => 2] ,
'2018-08-18' => ['male' => 3, 'female' => 3] ,
'2018-08-03' => ['male' => 2, 'female' => 0] ,
'2018-08-04' => ['male' => 4, 'female' => 3] ,
];
// construct day interval partitions in a month
$partitions = 2;
$s_day = 1;
$intervals = [];
while (end($intervals)['to'] != 30) {
$e_tmp = $s_day + (int)(30/$partitions) - 1;
$e_day = $e_tmp > 30 ? 30 : $e_tmp;
$intervals[] = ['from' => $s_day, 'to' => $e_day];
$s_day = $e_day + 1;
}
// partition info by intervals
$info_partitioned = [];
foreach ($data 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'];
}
}
var_dump($results);
分区 = 2,输出:
array(2) {
["2018-08 : 1-15"]=>
array(2) {
["male"]=>
int(17)
["female"]=>
int(6)
}
["2018-08 : 16-30"]=>
array(2) {
["male"]=>
int(6)
["female"]=>
int(5)
}
}
分区 = 4,输出:
array(3) {
["2018-08 : 1-7"]=>
array(2) {
["male"]=>
int(16)
["female"]=>
int(5)
}
["2018-08 : 8-14"]=>
array(2) {
["male"]=>
int(1)
["female"]=>
int(1)
}
["2018-08 : 15-21"]=>
array(2) {
["male"]=>
int(6)
["female"]=>
int(5)
}
}
必须选择截止日期和起始日期。 我有一个包含日期键的数组我想要的是如果我 select 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);
sample outputs
如果我对你的理解是正确的——你想将性别信息分成精确月份的相等分区,然后将这些分区中的性别计数相加。这是我想出的代码:
// gender data
$data = [
'2018-08-01' => ['male' => 3, 'female' => 0] ,
'2018-08-02' => ['male' => 1, 'female' => 0] ,
'2018-08-06' => ['male' => 6, 'female' => 2] ,
'2018-08-11' => ['male' => 1, 'female' => 1] ,
'2018-08-17' => ['male' => 3, 'female' => 2] ,
'2018-08-18' => ['male' => 3, 'female' => 3] ,
'2018-08-03' => ['male' => 2, 'female' => 0] ,
'2018-08-04' => ['male' => 4, 'female' => 3] ,
];
// construct day interval partitions in a month
$partitions = 2;
$s_day = 1;
$intervals = [];
while (end($intervals)['to'] != 30) {
$e_tmp = $s_day + (int)(30/$partitions) - 1;
$e_day = $e_tmp > 30 ? 30 : $e_tmp;
$intervals[] = ['from' => $s_day, 'to' => $e_day];
$s_day = $e_day + 1;
}
// partition info by intervals
$info_partitioned = [];
foreach ($data 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'];
}
}
var_dump($results);
分区 = 2,输出:
array(2) {
["2018-08 : 1-15"]=>
array(2) {
["male"]=>
int(17)
["female"]=>
int(6)
}
["2018-08 : 16-30"]=>
array(2) {
["male"]=>
int(6)
["female"]=>
int(5)
}
}
分区 = 4,输出:
array(3) {
["2018-08 : 1-7"]=>
array(2) {
["male"]=>
int(16)
["female"]=>
int(5)
}
["2018-08 : 8-14"]=>
array(2) {
["male"]=>
int(1)
["female"]=>
int(1)
}
["2018-08 : 15-21"]=>
array(2) {
["male"]=>
int(6)
["female"]=>
int(5)
}
}