0

I have 2 arrays like that:

Days Array

Array (
    [0] => 2018-01-01
    [1] => 2018-02-04
    [2] => 2018-02-15
    [3] => 2018-04-06
    [4] => 2018-04-12
    [5] => 2018-04-19
    [6] => 2018-04-27
    [7] => 2018-07-08
    [8] => 2018-08-12
    [9] => 2018-08-11
    [10] => 2018-08-21
    [11] => 2018-10-12
    [12] => 2018-10-13
    [13] => 2018-10-14
    [14] => 2018-10-15
    [15] => 2018-11-06
    [16] => 2018-12-12
    [17] => 2018-12-28
)

Datas Array

Array (
    [0] => 4
    [1] => 2
    [2] => 3
    [3] => 2
    [4] => 2
    [5] => 9
    [6] => 7
    [7] => 8
    [8] => 12
    [9] => 21
    [10] => 1
    [11] => 2
    [12] => 13
    [13] => 4
    [14] => 15
    [15] => 6
    [16] => 2
    [17] => 8
)

I need to group data(adding on to each other of the same month), and group also dates in months.

The expected result is a single array like that:

Array (
    [0] => Array ( [month] => 01 [data] => 4) 
    [1] => Array ( [month] => 02 [data] => 5) /* 2 + 3 = 5 */
    ...
    ...
)

Or two array one for months and one for data.

This is not a duplicate. This answer only work if months have only first day multiple times. And is also a lot tricky.

Luca Antonelli
  • 349
  • 1
  • 2
  • 21
  • 1
    What have you tried so far ? – Jules R Dec 18 '18 at 15:14
  • Possible duplicate of [Multidimensional Array- Grouping by month](https://stackoverflow.com/questions/19072804/multidimensional-array-grouping-by-month) – Masivuye Cokile Dec 18 '18 at 15:15
  • @JulesR i'm already searching i haven't find anything to start with, i think i could get the month from date, and i also need to iterate through a loop but i'm not completely sure. – Luca Antonelli Dec 18 '18 at 15:27

2 Answers2

2

You have to loop throught array and create a new array.

<?php
$arr1 = array ('2018-01-01', '2018-02-04', '2018-02-15', '2018-04-06', '2018-04-12', '2018-04-19', '2018-04-27', '2018-07-08', '2018-08-12', '2018-08-11', '2018-08-21', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-11-06', '2018-12-12', '2018-12-28');

$arr2 = array(4, 2, 3, 2, 2, 9, 7, 8, 12, 21, 1, 2, 13, 4, 15, 6, 2, 8);


$count1 = count($arr1);
$count2 = count($arr2);
$new_arr = array();

while($count1 > 0 and $count2 > 0){
    $count1--;
    $count2--;
    $data = $arr2[$count2];
    $month = $arr1[$count1];
    $month = explode("-", $month);
    $month = $month[1];
    $new_arr[] = ['month' => $month, 'data' => $data];
}

print_r($new_arr);

You can test it here : http://sandbox.onlinephpfunctions.com/code/bbf06c48b7242a8d3575610e0cec3ef378e84c4b

EDIT:

<?php
$arr1 = array ('2018-01-01', '2018-02-04', '2018-02-15', '2018-04-06', '2018-04-12', '2018-04-19', '2018-04-27', '2018-07-08', '2018-08-12', '2018-08-11', '2018-08-21', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-11-06', '2018-12-12', '2018-12-28');

$arr2 = array(4, 2, 3, 2, 2, 9, 7, 8, 12, 21, 1, 2, 13, 4, 15, 6, 2, 8);


$count1 = count($arr1);
$count2 = count($arr2);
$new_arr = array();

while($count1 > 0 and $count2 > 0){
    $count1--;
    $count2--;
    $data = $arr2[$count2];
    $month = $arr1[$count1];
    $month = explode("-", $month);
    $month = $month[1];
    $new_arr[] = ['month' => $month, 'data' => $data];
}

$temp = [];
foreach($new_arr as $value) {
    if(!array_key_exists($value['month'], $temp)) {
        $temp[$value['month']] = 0;
    }
    $temp[$value['month']] += $value['data'];
}

print_r($temp);

You can test it here : http://sandbox.onlinephpfunctions.com/code/7178c298da8422cf1e8f23ee230adece5999a914

executable
  • 3,365
  • 6
  • 24
  • 52
1

This is my different solution:

$array = array();
$var = -1;
$i = 0;

foreach($timestamp as $date) {
    $months = date("F", strtotime($date));
    if(!isset($temp) || $months != $temp) {
        ++$var;
        $array[$var]['data'] = $data[$i];
        $array[$var]['month'] = $months;
        ++$i;
    }
    else {
        $array[$var]['data'] = $array[$var]['data'] + $data[$i];
        ++$i;
    }
    $temp = $months;
}
Luca Antonelli
  • 349
  • 1
  • 2
  • 21