?
Solved

CSv Unique column A data while adding up column b data

Posted on 2009-05-04
6
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:lavinpj1
ID: 24297355
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
ID: 24297382
Yes thats the format

Column A , column B

pink,1
pink,3

etc
0
 
LVL 2

Expert Comment

by:lavinpj1
ID: 24297648
<?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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:danialbb
ID: 24297679
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 2000 total points
ID: 24297732
<?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
ID: 31577647
Excellent work Thanks Again
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 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