2

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

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.

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.

Thanks in advance!

helloandre
  • 10,541
  • 8
  • 47
  • 64
danielmesh
  • 119
  • 9

2 Answers2

1

i would loop through each array and

  1. create an array of possible 10-character matches
  2. if a match is found, add the rest of the columns to that match

This assumes that there can be multiple 10-character strings that can match:

$stats = <your array from above>
$output = array();

foreach($stats as $stat){
    // first we create an element in the array so we can sum
    $key = substr($stat['WBS Element'], 0, 10);
    if (!array_key_exists($stat['WBS Element'], $output)){
        $output[$key] = array();
    }

    // sum up rest of columns based on $output[$key]
    // or simply create them if the if-statement was matched above
    $output[$key]['CYSpent'] += $stat['CYSpent'];
    // etc
}

this will give you an output of

array(
    [10-char-key-1] => array(
        // columns
    )
    [10-char-key-2] => array(
        // columns
    )
    // etc
)
helloandre
  • 10,541
  • 8
  • 47
  • 64
  • Thanks so much for the quik reply, it works great! I just can't figure out how to now sum the column. The column I am working with is "CY Actuals". Let me know if you need more information :-) Thanks again for the help, this is brilliant! I am loving PHP – danielmesh Apr 28 '11 at 20:00
  • With the code above with the $output Key of "CY Actuals" I get the following result. Array ( [1234567.01] => Array ( [CY Actuals] => 579373 ) [1234567.02] => Array ( [CY Actuals] => 22756 ) ) I need to get this result: Array ( [1234567.01] => Array ( [CY Actuals] => 579373 ) [1234567.02] => Array ( [CY Actuals] => 120195 ) ) – danielmesh Apr 28 '11 at 20:06
1
$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...
}
AndreKR
  • 32,613
  • 18
  • 106
  • 168