Solved

CSv Unique column A data while adding up column b data

Posted on 2009-05-04
6
196 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
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 …

770 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