-1

To-date and from-date has to be chosen. I have one array having key as date what i want is if i select 15 days key values up to 15 will be adding and come to one column so if my to-date and from-date difference is 30 so it will come two column 1-15 / 16-30 and the values will be added and sit according

    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

Tann
  • 15
  • 1
  • 5
  • 2
    We need to see your coding attempt and exact desired output for both conditional scenarios. This is Too Broad. – mickmackusa Aug 23 '18 at 07:37
  • Where is your code ? And you only posted your data. As i understood you need some way of aggregating / partitioning of data according to some input. In this case you need to show a) Sample input b) Expected output on sample input – Agnius Vasiliauskas Aug 23 '18 at 07:39
  • @mickmackusa I just confused and put my question here.I have my array out put but i am not getting understand how to get calculated according to key value – Tann Aug 23 '18 at 08:19
  • Please show your attempt to self-solve; it is okay to post non-working code in a question. We can see your input data, but you have not shown your exact desired output. – mickmackusa Aug 23 '18 at 08:27
  • I have added please suggest how can i add the values – Tann Aug 23 '18 at 09:03
  • @AgniusVasiliauskas I have shared the image in this link https://i.stack.imgur.com/qKtxJ.jpg – Tann Aug 23 '18 at 09:11
  • @Taniya just so you know, your accepted solution is inefficient for many reasons. Not only have you failed to post your failed coding attempt, you have also failed to express your exact desired output. This means that volunteers have to guess what result you want. You realize that some months have 31 days right? How do you want the outcome to look when you have multiple months (like in your commented image)? This question is Too Broad because there are many ways to solve it and the issue has not been narrowed down -- it is a effectively a plea for free work to be done for you. – mickmackusa Sep 03 '18 at 02:29

1 Answers1

0

If I correctly understood you - you want to split gender info into equal partitions of the exact month and sum these gender counts in partitions. This is the code which I came up with:

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

Partitions = 2, outputs :

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

Partitions = 4, outputs:

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

Try it online!

Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70