Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维数组求和并根据键值放入单列_Php_Arrays_Multidimensional Array_Array Sum - Fatal编程技术网

Php 多维数组求和并根据键值放入单列

Php 多维数组求和并根据键值放入单列,php,arrays,multidimensional-array,array-sum,Php,Arrays,Multidimensional Array,Array Sum,必须选择“截止日期”和“起始日期”。 我有一个数组,其中键为date,我想要的是,如果我选择15天,最多15天的键值将被添加到一列,因此如果我的“截止日期”和“起始日期”差为30,那么它将出现两列1-15/16-30,值将被添加并根据 Array ( [2018-08-01] => Array ( [male] => 3 [female] => 0

必须选择“截止日期”和“起始日期”。 我有一个数组,其中键为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); 
数组
(
[2018-08-01]=>阵列
(
[男]=>3
[女性]=>0
)
[2018-08-02]=>阵列
(
[男]=>1
[女性]=>0
)
[2018-08-06]=>阵列
(
[男]=>6
[女性]=>2
)
[2018-08-11]=>阵列
(
[男]=>1
[女性]=>1
)
[2018-08-17]=>阵列
(
[男]=>3
[女性]=>2
)
[2018-08-18]=>阵列
(
[男]=>3
[女性]=>3
)
[2018-08-03]=>阵列
(
[男]=>2
[女性]=>0
)
[2018-08-04]=>阵列
(
[男]=>4
[女性]=>3
)
)
1-15个日期将得到一个和值,16-30将得到另一个和值,那么有多少个月是这样的,它将以1-15/16-30的方式出现
这个答案非常有效
$partitions=2;
$s_day=1;
$interval=[];
while(结束($interval)[‘到’!=31){
$e_tmp=$s_day+(int)(31/$partitions)-1;
$e_day=$e_tmp>31?31:$e_tmp;
$interval[]=['从'=>$s_-day'到'=>$e_-day];
$s_日=$e_日+1;
}
//按间隔划分的分区信息
$info_partitioned=[];
foreach($generaresult as$date=>$info){
//解析月份和日期
预匹配('/(\d+-\d+)(\d+/”,$date,$matches);
//选择间隔
$found=null;
foreach(间隔为$days){
如果($matches[2]>=$days['from']&&$matches[2]$counts){
foreach($stat计为$stat){
$results[$partition]['male']+=$stat['male'];
$results[$partition]['female']+=$stat['female'];
}
}
打印(结果);

如果我正确理解您的意思-您希望将性别信息拆分为准确月份的相等分区,并将这些分区中的性别计数相加。这是我提出的代码:

// 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);
分区=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)
  }
}

我们需要查看您的编码尝试和两种条件场景的准确期望输出。这太广泛了。您的代码在哪里?您只发布了数据。据我所知,您需要根据一些输入对数据进行聚合/分区。在这种情况下,您需要在样本上显示a)样本输入b)预期输出input@mickmackusa我只是把我的问题弄糊涂了。我已经把我的数组放出来了,但我不明白如何根据键值进行计算。请展示你自己解决问题的尝试;在问题中发布不起作用的代码是可以的。我们可以看到你的输入数据,但你没有显示你确切的期望输出。我已经添加了请建议如何添加这些值
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)
  }
}