How to sum array data in json specified by date

3
array(10) {
  [0]=>
  array(4) {
    ["data"]=>
    string(10) "01/11/2015"
    ["valor"]=>
    int(50)
    ["intensidade"]=>
    float(37.5)
    ["carga"]=>
    int(35)
  }
  [1]=>
  array(4) {
    ["data"]=>
    string(10) "01/11/2015"
    ["valor"]=>
    int(52)
    ["intensidade"]=>
    float(33.8)
    ["carga"]=>
    int(60)
  }
  [2]=>
  array(4) {
    ["data"]=>
    string(10) "01/11/2015"
    ["valor"]=>
    int(25)
    ["intensidade"]=>
    float(21.75)
    ["carga"]=>
    int(15)
  }
  [3]=>
  array(4) {
    ["data"]=>
    string(10) "02/11/2015"
    ["valor"]=>
    int(52)
    ["intensidade"]=>
    float(33.8)
    ["carga"]=>
    int(60)
  }
[4]=>
  array(4) {
    ["data"]=>
    string(10) "02/11/2015"
    ["valor"]=>
    int(52)
    ["intensidade"]=>
    float(33.8)
    ["carga"]=>
    int(60)
  }
[5]=>
  array(4) {
    ["data"]=>
    string(10) "05/11/2015"
    ["valor"]=>
    int(52)
    ["intensidade"]=>
    float(33.8)
    ["carga"]=>
    int(60)
  }
}

So, I have this array and I need to calculate the data of the same date and divide by how many times it appears, after that I need to regenerate the array with the new data.

    
asked by anonymous 31.10.2015 / 22:05

1 answer

0
for ($i=0; $i < sizeof($arrays)-1; $i++) {
    for ($j=1; $j < sizeof($arrays); $j++) { 
        if( ($arrays[$i]['data'] === $arrays[$j]['data']) &&  !isset($arrays[$i]['sum'])  && !isset($arrays[$j]['sum']) ){
            if($i != $j){
                $arrays[$i]['valor'] += $arrays[$j]['valor'];
                $arrays[$i]['intensidade'] += $arrays[$j]['intensidade'];
                $arrays[$i]['carga'] += $arrays[$j]['carga'];   
                $arrays[$j]['sum'] = true;
                $delete[] = $j;
            }
        }
    }
}

for ($i=0; $i < sizeof($delete); $i++) {
    unset($arrays[$delete[$i]]);
}

Where $arrays is your array . Note: it is not the perfect solution but gives you a north.

    
02.11.2015 / 00:21