Solved

CSv Unique column A data while adding up column b data

Posted on 2009-05-04
6
197 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to use a switch statement with heredoc 11 33
What do the parameters in the PDO function debugDumpParams mean? 1 34
XML extra information 8 28
php help 34 58
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

791 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