ISAB Posted April 17, 2013 Share Posted April 17, 2013 Hi everyone. I have a csv file, im trying to read and output the number of unique row according to second column. csv file : 2013-03-25 00:00:06,"00:0E:6D:3F:D5:76","-68","5374468","B" 2013-03-25 00:00:07,"04:A8:2A:18:8A:04","-66","5898756","B" 2013-03-25 00:00:08,"00:0E:6D:3F:D5:76","-68","5374468","B" so the output of the csv file should be 2. I appreciate any help thanks . Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 17, 2013 Share Posted April 17, 2013 This might work: $file = file_get_contents('file.csv'); $lines = explode("\n", $file); $buckets = array(); foreach ($lines as $line) { $items = explode(',', $line); $c2 = $items[1]; if (!isset($buckets[$c2])) $buckets[$c2] = 1; else $buckets[$c2]++; } krsort($buckets); echo key($buckets); It will break if you get commas inside the quotes, but assuming that data is consistent, it shouldn't matter. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 17, 2013 Share Posted April 17, 2013 or use array_count_value() $f = fopen('test.csv','r'); while ($line=fgetcsv($f, 1024)) { $arr[] = $line[1]; } $counts = array_count_values($arr); asort($counts); // sort least occuring to top echo key($counts); fclose($f); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.