Question: Simple code like this:

```
        $array = [
            [11 => 771725],
            [11 => 847226],
            [10 => 410035],
            [11 => 455387],
        ];
        dd($array);

```

output:

[![enter image description here](https://i.stack.imgur.com/1n1ec.png)](https://i.stack.imgur.com/1n1ec.png)

The result in OCTOBER (10) there is 1 price:

```
["410035"]

```

while in NOVEMBER (11) there are 3 prices:

```
["771725", "847226", "455387"]

```

and at DECEMBER (12) is none

I need logic to calculate all prices in months 10, 11, and 12.
The expected output is

```
[
    10 => "2074338",
    11 => "410035",
    12 => "0"
]

```

Thanks :)

Answer: 
I used [Collection](https://laravel.com/docs/8.x/collections#introduction) and end up with the correct results. This could be achieve in a number of ways:

**APPROACH 1:**

```php
collect($array)
    ->groupBy(function ($item) {
        return collect($item)->keys()->first();
    })
    ->map(function ($items) {
        return collect($items)->flatten()->sum();
    });

```

**APPROACH 2:**

```php
collect($array)
    ->groupBy(function ($item) {
        return array_key_first($item);
    })
    ->map(function ($items) {
      return collect($items)->flatten()->sum();
    });

```

**APPROACH 3:**

```php
$default = [
    1 => 0, 
    2 => 0,
    3 => 0,
    4 => 0,
    5 => 0,
    6 => 0,
    7 => 0,
    8 => 0,
    9 => 0,
    10 => 0,
    11 => 0,
    12 => 0,
];

collect($array)
    ->reduce(function ($carry, $item) {
        $month = array_key_first($item);

        $carry[$month] += $item[$month];

        return $carry;
    }, $default);

```
Please see my approach below

**As asked...**

```
$array = [
            [11 => 771725],
            [11 => 847226],
            [10 => 410035],
            [11 => 455387],
        ];

$monthTotals = [
            10 => 0, 
            11 => 0, 
            12 => 0
        ];

foreach ($array as $value) {
    foreach($value as $key => $amount)
    {
        if(isset($monthTotals[$key]))
        {
            $monthTotals[$key] += $amount;
        }
    }
}

print_r($monthTotals);

```

**Suggested...**

Note: Since you posted this in September, I'm assuming from the context that you're looking for the next 3 months of data. The below will always show you the data from the next 3 months...

```
$array = [
            [11 => 771725],
            [11 => 847226],
            [10 => 410035],
            [11 => 455387],
        ];

$datePlus1 = date('m', strtotime('+1 month'));
$datePlus2 = date('m', strtotime('+2 month'));
$datePlus3 = date('m', strtotime('+3 month'));

$monthTotals = [
            $datePlus1 => 0, 
            $datePlus2 => 0, 
            $datePlus3 => 0
        ];

foreach ($array as $value) {
    foreach($value as $key => $amount)
    {
        if(isset($monthTotals[$key]))
        {
            $monthTotals[$key] += $amount;
        }
    }
}

print_r($monthTotals);

```