简体   繁体   English

如何获得相互匹配的数组值的总和?

[英]How can I get the sum of array values that match each other?

For particular reasons, I won't be doing this in a SQL query even though it seems that would make sense.出于特殊原因,我不会在 SQL 查询中执行此操作,尽管这似乎是有道理的。 I would like to manipulate this data in PHP.我想在 PHP 中操作这些数据。

I am outputting results using PHP to a CSV file.我使用 PHP 将结果输出到 CSV 文件。 Those CSV results look like this:这些 CSV 结果如下所示:

Event Date          Client      Event Type      Location        Player      Pay      Total
2020/05/30 7:00 PM  Erin        Public Event    Effingham       Davy T      10
2020/05/01 9:00 PM  Mike        Fundraiser      Lakewood        Greg P      100
2020/06/13 1:30 AM  Jeff        Wedding         Lakewood        Greg P      60
2020/05/30 7:00 PM  Erin        Public Event    Effingham       Katrina N   0
2020/05/01 9:00 PM  Mike        Fundraiser      Lakewood        Michael S   400
2020/06/12 1:30 AM  Jeff        Wedding         Lakewood        Michael S   50
2020/06/13 1:30 AM  Jeff        Wedding         Lakewood        Michael S   61
2020/03/14 5:00 PM  Kimberly    Corporate       Aurora          Robert D    12.5
2020/05/01 9:00 PM  Mike        Fundraiser      Lakewood        Robert D    450
2020/05/30 7:00 PM  Erin        Public Event    Effingham       Robert D    0
2020/06/12 1:30 AM  Jeff        Wedding         Lakewood        Robert D    51

I want to sum the values where the names match, so that it would look like this:我想对名称匹配的值求和,使其看起来像这样:

Event Date          Client      Event Type      Location        Player      Pay      Total
2020/05/30 7:00 PM  Erin        Public Event    Effingham       Davy T      10       10
2020/05/01 9:00 PM  Mike        Fundraiser      Lakewood        Greg P      100      160
2020/06/13 1:30 AM  Jeff        Wedding         Lakewood        Greg P      60       160
2020/05/30 7:00 PM  Erin        Public Event    Effingham       Katrina N   0        0
2020/05/01 9:00 PM  Mike        Fundraiser      Lakewood        Michael S   400      511
2020/06/12 1:30 AM  Jeff        Wedding         Lakewood        Michael S   50       511
2020/06/13 1:30 AM  Jeff        Wedding         Lakewood        Michael S   61       511 
2020/03/14 5:00 PM  Kimberly    Corporate       Aurora          Robert D    12.5     513.5
2020/05/01 9:00 PM  Mike        Fundraiser      Lakewood        Robert D    450      513.5
2020/05/30 7:00 PM  Erin        Public Event    Effingham       Robert D    0        513.5
2020/06/12 1:30 AM  Jeff        Wedding         Lakewood        Robert D    51       513.5

My current PHP code for outputting the query is:我当前用于输出查询的 PHP 代码是:

$csv_output = '"'.implode('","',array_keys($results[0])).'",Total'."\n";;


  foreach ($results as $row) {
    print_r($row);
    $csv_output .= '"'.implode('","',$row).'",'."\n";
  }

Here is an example of the array output by adding print_r into the foreach loop:这是通过将 print_r 添加到 foreach 循环中的数组 output 的示例:

Array
(
    [Event Date] => 2020/02/21 05:00 PM
    [Client] => Jeff
    [Event Type] => Wedding
    [Location] => Lakewood
    [Player] => Greg P
    [Pay] => 60
)
Array
(
    [Event Date] => 2020/05/30 07:00 PM
    [Client] => Erin
    [Event Type] => Public Event
    [Location] => Effingham
    [Player] => Davy
    [Pay] => 10
)
Array
(
    [Event Date] => 2020/05/01 09:00 PM
    [Client] => Mike
    [Event Type] => Fundraiser
    [Location] => Lakewood
    [Player] => Greg
    [Pay] => 100
)

How can I gather the sum amount of 'Pay' and put that into the total fields where the array values match each other as shown in the desired csv output above?如何收集“支付”的总金额并将其放入数组值相互匹配的总字段中,如上面所需的 csv output 所示?

You must iterate over array two times.您必须遍历数组两次。 First for counting sums of pays, and second to create new element of array.首先用于计算支付总和,其次用于创建数组的新元素。

$players = [
    [
        'Player' => 'Greg P',
        'Pay' => 60
    ],
    [
        'Player' => 'Greg P',
        'Pay' => 10
    ],
    [
        'Player' => 'Davy',
        'Pay' => 60
    ]
];

$pays = [];

foreach ($players as $player) {
    $pays[$player['Player']] = ($pays[$player['Player']] ?? 0) + $player['Pay'];
}

foreach (array_keys($players) as $playerKey) {
    $players[$playerKey]['Total'] = $pays[$players[$playerKey]['Player']];
}

Output for my example: Output 以我为例:

Array
(
    [0] => Array
        (
            [Player] => Greg P
            [Pay] => 60
            [Total] => 70
        )

    [1] => Array
        (
            [Player] => Greg P
            [Pay] => 10
            [Total] => 70
        )

    [2] => Array
        (
            [Player] => Davy
            [Pay] => 60
            [Total] => 60
        )

)

You can calculate sum for each client using array_walk and second array as [Client => Sum]您可以使用 array_walk 和第二个数组作为 [Client => Sum] 计算每个客户端的总和

<?php

$array = [
    [
        'Client' => 'x',
        'Pay' => 1
    ],
    [
        'Client' => 'y',
        'Pay' => 1
    ],
    [
        'Client' => 'x',
        'Pay' => 5
    ]
];

$tempValues = [];

array_walk($array, function($row) use (&$tempValues) {
    if (!isset($tempValues[$row['Client']])) {
        $tempValues[$row['Client']] = 0;
    }
    $tempValues[$row['Client']] += $row['Pay'];
});

this will give you:这会给你:

array(2) {
  ["x"]=>
  int(6)
  ["y"]=>
  int(1)
}

and then include $tempValues[$row['Client']] to your csv output.然后将$tempValues[$row['Client']]包含到您的 csv output 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM