How to sum array data in json specified by date

Asked

Viewed 354 times

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 as many times as it appears, after that I need to generate again the array with the new data.

  • And what you tried to do so far?

  • Well, all this was in one array, I used the foreach to break and identify each array, then tried to do some things and nothing worked.

  • I know I need to do at the end an array containing 3 arrays inside each with its date.

  • Try to explain the context of your problem better. Your array comes from somewhere, where? The way you expose your doubt can help you a lot to get good answers. You can [Dit] your question to make it clear but after reading [Ask] and the [tour].

  • The question is very explanatory, I have an array, this array comes from the database, I’m pulling these values, but sometimes there is equal data in matter of date, I just need to add them because these values are placed in a graph, and is appearing several times the same day.

  • 1

    Her last comment gives a very important context to your question that is not in her body: the data comes from the database. You can directly average per date in your query by eliminating the extra step of having to deal with it in the application. If your array came from a webservice where you can’t do that, that would be another approach. You know what I mean about the lack of context in the question?

  • No, it cannot be done, because all the data coming from there is already filtered and done the math.

Show 2 more comments

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 more te a north.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.