PHP从两个数组中获取每个月销售总额

huangapple go评论63阅读模式
英文:

PHP Get sum of each month's sales from two arrays

问题

Array 1:

['Apr 2023', 'Mar 2023', 'Apr 2023', 'Apr 2023', 'Feb 2023']

Array 2:

[833.00, 223.00, 829.00, 220.00, 498.00]

我需要为每个月找到总数:

Apr 2023: 1882.00

Mar 2023: 223.00

Feb 2023: 498.00

我尝试将它们组合成一个多维数组,然后使用array_reduce,但是我一直收到类型错误。我尝试编写一系列函数来逐步处理数据,但我无法弄清如何与前一个条目比较月/年。似乎我漏掉了一个明显的答案... 有什么想法吗?

对于那些可能会说这个问题以前已经被多次回答的人,我已经在网络和Stack Overflow上搜索了几个小时,没有找到任何接近的答案。

谢谢!

英文:

I have two arrays where the keys are the same, one with the month/year and the other with the values:

Array 1:

['Apr 2023', 'Mar 2023', 'Apr 2023', 'Apr 2023', 'Feb 2023'] 

Array 2:

[833.00, 223.00, 829.00, 220.00, 498.00]

I need to find the totals for each month:

Apr 2023: 1882.00

Mar 2023: 223.00

Feb 2023: 498.00

I have tried combining into one multidimensional array and then using array_reduce, but I keep getting type errors. I have tried writing a series of functions to step through the data, but I can't figure out how to compare the month/year with the previous entry. It seems that I'm missing an obvious answer... Any thoughts?
And to those who might say that this has previously been answered multiple times, I have been searching the web and SO for several hours and haven't come up with anything close.
Thank you!

答案1

得分: 3

遍历months数组,并使用该索引从另一个数组中获取金额,将其加到该月份的总和。在https://3v4l.org/uItTB上查看实际示例。

$months = [
    '2023-04',
    '2023-03',
    '2023-04',
    '2023-04',
    '2023-02',
];

$amounts = [
    833,
    223,
    829,
    220,
    498
];

$sums = [];

foreach($months as $i => $month) {
    if (!isset($sums[$month])) {
        $sums[$month] = 0;
    }
    
    $sums[$month] += $amounts[$i];
}

var_dump($sums);

输出:

array(3) {
  ["2023-04"] =>
  int(1882)
  ["2023-03"] =>
  int(223)
  ["2023-02"] =>
  int(498)
}
英文:

Iterate over the months array, and use that index to get the amount from the other array, and add that to the month's sum. Live example at https://3v4l.org/uItTB

<?php

$months = [
    '2023-04',
    '2023-03',
    '2023-04',
    '2023-04',
    '2023-02',
];

$amounts = [
    833,
    223,
    829,
    220,
    498
];

$sums = [];

foreach($months as $i => $month) {
    if (!isset($sums[$month])) {
        $sums[$month] = 0;
    }
    
    $sums[$month] += $amounts[$i];
}

var_dump($sums);

outputs:

array(3) {
  ["2023-04"]=>
  int(1882)
  ["2023-03"]=>
  int(223)
  ["2023-02"]=>
  int(498)
}

Note that the main functionality in the loop is just the line $sums[$month] += $amounts[$i];. The if (!isset($sums[$month])) before that is purely to avoid a PHP warning about the array index being undefined before trying to add a value to it - Warning: Undefined array key "2023-04". You could remove that conditional and it would still function the same. And if you wanted to suppress the warning too, you could use the @ operator: @$sums[$month] += $amounts[$i];.

huangapple
  • 本文由 发表于 2023年5月25日 23:22:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定