-1

必须选择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); 

样本输出

4

1 回答 1

0

如果我正确理解您 - 您想将性别信息拆分为确切月份的相等分区,并将这些性别计数汇总到分区中。这是我想出的代码:

// 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)
  }
}

在线尝试!

于 2018-08-23T13:17:31.197 回答