问题描述
我有一个从CSV文件创建的数组.该数组包含以下内容.基本上有四行六列. IE.它是多维的.
I have an array that was created from a CSV file. The array contains the following. Basically it has four lines and six columns. I.E. it is multi dimensional.
Array (
[1] => Array (
[WBS Element] => 1234567.01
[Proj System Status] =>
[CY Actuals] => 579373
[ITD Actuals] => 696,609
[Overall Commitment] =>
[Overall Assigned] => 696,609
[CYSpent] => 579,373 )
[2] => Array (
[WBS Element] => 1234567.02
[Proj System Status] =>
[CY Actuals] => 86689
[ITD Actuals] => 86,689
[Overall Commitment] =>
[Overall Assigned] => 86,689
[CYSpent] => 86,689 )
[3] => Array (
[WBS Element] => 1234567.02.01
[Proj System Status] =>
[CY Actuals] => 10750
[ITD Actuals] => 86,689
[Overall Commitment] =>
[Overall Assigned] => 86,689
[CYSpent] => 86,689 )
[4] => Array (
[WBS Element] => 1234567.02.02
[Proj System Status] =>
[CY Actuals] => 22756
[ITD Actuals] => 86,689
[Overall Commitment] =>
[Overall Assigned] => 86,689
[CYSpent] => 86,689 )
)
您会注意到我的一个键"WBS Element"中有一个值,其中前十个字符可能与数组中的另一行匹配.我需要完成的是获取"WBS元素"的前十个字符匹配的任何行,并将其他列相加,以便结果是具有相同列的聚合数组,但没有前十个字符匹配的行.
You will notice that one of my keys "WBS Element" has a value in it where the first ten characters might match another row in the array. What I need to accomplish is to take any row where the first ten characters of the "WBS Element" match and sum the other columns together so that the result is a aggregated array with the same columns but no rows with the first ten characters matching.
希望这是有道理的,我正在努力实现.我是PHP的新手,所以对您的帮助将不胜感激.我已经获得了列求和功能,但是我不知道要在数组中搜索匹配"键,然后通过求和将它们组合在一起.
Hopefully that makes sense what I am trying to accomplish. I am new when it comes to PHP so any help would be appreciated. I ahve gotton a column summerization to work but I can't figure out to search an array for "matching" keys then combine those together by summing.
提前谢谢!
推荐答案
$new_array = array();
foreach ($array as $row)
{
$key = substr($row['WBS Element'],0,10);
$new_array[$key]['WBS Element'] = $key; // optional
$new_array[$key]['Proj System Status'] += $row['Proj System Status'];
$new_array[$key]['CY Actuals'] += $row['CY Actuals'];
$new_array[$key]['ITD Actuals'] += $row['ITD Actuals'];
// same for Overall Commitment, etc...
}
这篇关于在PHP中,在多维数组中找到重复的条目,然后对数组的特定键中的值求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!