Solved

CSv Unique column A data while adding up column b data

Posted on 2009-05-04
6
194 Views
Last Modified: 2013-12-13
I have to 2 columns of information in a CSV file example

Column A       Column B (stats)

pink                      2
pink                      3
red                       1
red                       6
red                       3
black                    1
black                    5
black                    2

the results I am after are Column A. unique words only but at the same time to add up the stats with matching keyword stats

The results should be

pink                5    
red                 10
black              8

If it could be written as another CSV file it would be appeciated
0
Comment
Question by:danialbb
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:lavinpj1
Comment Utility
Hello,

Can you post an example of the CSV format you're working with? I assume it's something like:

pink,2
pink,3
red,1
red,6

but clarification would be handy.

Phil
0
 

Author Comment

by:danialbb
Comment Utility
Yes thats the format

Column A , column B

pink,1
pink,3

etc
0
 
LVL 2

Expert Comment

by:lavinpj1
Comment Utility
<?php
$filename = '/home/phil/file.csv';

// Read the file
$handle = fopen($filename, 'r');
$contents = trim(fread($handle, filesize($filename)));

// Array for output
$output = array();

// Split by \n and loop
$lines = explode("\n", $contents);

foreach ($lines as $line) {
      // Split by , and enumerate
      $line = rtrim($line);
      $temp = explode(',', $line);

      $output[$temp[0]] += $temp[1];
}

// Loop and output
foreach ($output as $key=>$val) {
      echo "{$key},{$val}\n";
}
?>

That reads from a file and outputs it to the page/console. Let me know if you had other intentions for input/output.

Phil
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:danialbb
Comment Utility
Could you write to a csv file please. And I will accept the answer once tested.

Thanks for this.
0
 
LVL 2

Accepted Solution

by:
lavinpj1 earned 500 total points
Comment Utility
<?php
$inFilename = '/home/phil/file.csv';
$outFilename = '/home/phil/outfile.csv';

// Read the file
$handle = fopen($inFilename, 'r');
$contents = trim(fread($handle, filesize($inFilename)));
fclose($handle);

// Array for output
$output = array();

// Split by \n and loop
$lines = explode("\n", $contents);

foreach ($lines as $line) {
      // Split by , and enumerate
      $line = rtrim($line);
      $temp = explode(',', $line);

      $output[$temp[0]] += $temp[1];
}

// Open output file
$handle = fopen($outFilename, 'w+');

// Loop and output
foreach ($output as $key=>$val) {
      fwrite($handle, "{$key},{$val}\n");
}

fclose($handle);
?>


Phil
0
 

Author Closing Comment

by:danialbb
Comment Utility
Excellent work Thanks Again
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now