0

Simple code like this:

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

output:

enter image description here

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 :)

3 Answers 3

2

I used Collection and end up with the correct results. This could be achieve in a number of ways:

APPROACH 1:

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

APPROACH 2:

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

APPROACH 3:

$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);
Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

1

You could use a single foreach and pre-set and array with the months 1-12 as the key and 0 for the value using array_fill_keys.

In the foreach you can get the first key and value using key and reset.

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

$result = array_fill_keys(range(1,12), 0);
foreach ($array as $a) {
    $result[key($a)] += reset($a);
}

print_r($result);

Output

Array
(
    [1] => 0
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 0
    [8] => 0
    [9] => 0
    [10] => 410035
    [11] => 2074338
    [12] => 0
)

Php demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.