Add values for same-day array

Asked

Viewed 267 times

1

I have the following array:

    array (size=3)
  10 => 
    array (size=13)
      '11/06' => int 104
      '18/06' => int 145
      '25/06' => int 136
      '02/07' => int 135
      '09/07' => int 122
      '16/07' => int 133
      '23/07' => int 136
      '30/07' => int 139
      '06/08' => int 138
      '13/08' => int 140
      '20/08' => int 170
      '27/08' => int 162
      '03/09' => int 150
  'p10' => 
    array (size=13)
      '11/06' => float 20.04
      '18/06' => float 27.94
      '25/06' => float 26.2
      '02/07' => float 26.01
      '09/07' => float 23.51
      '16/07' => float 25.63
      '23/07' => float 26.2
      '30/07' => float 26.78
      '06/08' => float 26.59
      '13/08' => float 26.97
      '20/08' => float 32.76
      '27/08' => float 31.21
      '03/09' => float 28.9
  20 => 
    array (size=13)
      '11/06' => int 40
      '18/06' => int 41
      '25/06' => int 42
      '02/07' => int 39
      '09/07' => int 51
      '16/07' => int 73
      '23/07' => int 43
      '30/07' => int 61
      '06/08' => int 72
      '13/08' => int 61
      '20/08' => int 102
      '27/08' => int 63
      '03/09' => int 70

and what I need to do is add up all the values where the day and month are equal in the 3 arrays.

Of the type where 06/11 will have the value of 164.04.

I tried to put something like this together, but I’m really lost on how.

    $arr = array();
    foreach ($teste as $key => $item) {

    $arr[$key] += $item;

 }

  var_dump($arr)
  • Will all three array always have the same size, the same keys and always be sorted? I ask because I thought of a solution for arrays that meet these three criteria and another for case one or more of these criteria are not met.

2 answers

1


You can do it by making two foreach and in the second you add up using the $key.

$arr = [
    10 => [
      '11/06' => 104,
      '18/06' => 145,
      '25/06' => 136,
      '02/07' => 135,
      '09/07' => 122,
      '16/07' => 133,
      '23/07' => 136,
      '30/07' => 139,
      '06/08' => 138,
      '13/08' => 140,
      '20/08' => 170,
      '27/08' => 162,
      '03/09' => 150,   
    ],
    'p10' => [
      '11/06' => 20.04,
      '18/06' => 27.94,
      '25/06' => 26.2,
      '02/07' => 26.01,
      '09/07' => 23.51,
      '16/07' => 25.63,
      '23/07' => 26.2,
      '30/07' => 26.78,
      '06/08' => 26.59,
      '13/08' => 26.97,
      '20/08' => 32.76,
      '27/08' => 31.21,
      '03/09' => 28.9,
    ],
  20 => 
    [
      '11/06' => 40,
      '18/06' => 41,
      '25/06' => 42,
      '02/07' => 39,
      '09/07' => 51,
      '16/07' => 73,
      '23/07' => 43,
      '30/07' => 61,
      '06/08' => 72,
      '13/08' => 61,
      '20/08' => 102,
      '27/08' => 63,
      '03/09' => 70,
    ]
];

$totals = [];
foreach ($arr as $value) {
    foreach ($value as $key => $val) {
        $totals[$key] = isset($totals[$key]) ? $totals[$key] + $val : $val;
    }
}

echo '<pre>'; print_r($totals); die;

\\ Resultado
<pre>Array
(
    [11/06] => 164.04
    [18/06] => 213.94
    [25/06] => 204.2
    [02/07] => 200.01
    [09/07] => 196.51
    [16/07] => 231.63
    [23/07] => 205.2
    [30/07] => 226.78
    [06/08] => 236.59
    [13/08] => 227.97
    [20/08] => 304.76
    [27/08] => 256.21
    [03/09] => 248.9
)

0

You have an array with 3 arrays inside.
You can use array_keys to return all keys in $k and so only popular an array $sums with all the keys.
Then just use one foreach running on each item $k and add up all values.

<?php
$arr = [
    10 => [
      "11/06" => 2,
      "12/06" => 3,
    ],
   'p10' => [
      "11/06" => 1,
      "13/06" => 7,
    ],
   20 => [
    "11/06" => 3,
    "13/06" => 2,
    "14/06" => 5,
    ],
];
$sums = array();
foreach (array_keys($arr[10] + $arr['p10'] + $arr[20]) as $k) {
    $sums[$k] = @($arr[10][$k] + $arr['p10'][$k] + $arr[20][$k]);
}
print_r($sums);
?>

--outworking:--

Array
(
    [11/06] => 6
    [12/06] => 3
    [13/06] => 9
    [14/06] => 5
)

Browser other questions tagged

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