Jump to content

How to Aggregate Values from CSV file


Go to solution Solved by RobertAmsterdam,

Recommended Posts

Hello,

 

I have a CSV file that looks like this:

"Account","State","Zip","Email","Birth Date","Favorites","Balance"

with data rows in them. I need to calculate total balances aggregated by state, need to get output in .txt flle like:

State, Balance
value, value
this is how I started :
$inputfile = 'input.csv';
$inputHandle = fopen($inputfile, "r");
$output = array();
while (($data = fgetcsv($inputHandle, 1024, ",")) !== FALSE){

foreach($data as $line) {
    list($state, $balance) =  $data; 
    
    $state = trim($state);
    
    $balance = trim($balance);
    
    $output[$state] += (int)$balance;
    
    file_put_contents('summary.txt', var_export($output, TRUE));
    
 }
}
I get unidentified inxex errors on
 
$output[$state] += (int)$count;
 
May be i make some mistake in line:  
 
list($state, $balance) =  $data; 
    

 

 

Could someone maybe propose a better, more convinient solution here?

 

 

 

 

 

Link to comment
Share on other sites

You can't do a '+=' operation on something that hasn't been created yet.

You should also check that balance is not null before trying to  include it.

 

You should re-think your algorithm.  You are outputting your file on each and every pass thru the loop.  Wasteful.  Do it after your loop completes.

Link to comment
Share on other sites

  • Solution

There is like 12 rows, should I list each and every one of them, when I need only 2?

 

I could do like this:

$inputfile = 'input.csv';
$inputHandle = fopen($inputfile, "r");
$output = array();
while (($data = fgetcsv($inputHandle, 1024, ",")) !== FALSE){
    
   unset($data[0],$data[1],$data[2], $data[3], $data[4], $data[6], $data[7], $data[8], $data[9], $data[10], $data[11] );  // remove redundant fields
    
   $data = array_values($data);  // restore keys
}
foreach($data as $key => $line) {
    
    list($state, $balance) =  $line;
    $state = trim($state);
    $balance = trim($balance);
    $output[$state] += (int)$balance;
    
    //file_put_contents('summary.txt', var_export($output, TRUE));
  }


  file_put_contents('summary.txt', var_export($output, TRUE));

but i get  Invalid argument supplied for foreach() 

for some reason

Link to comment
Share on other sites

$state = $dataRow[5];
    $balance =  $dataRow[12];
    if (!isset($sumArray[$state])) {
        $sumArray[$state] = 0; 
    }
    $sumArray[$state] += $balance; // add balance for state

was solution

Edited by RobertAmsterdam
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.