Jump to content

Create array from csv file.


ISAB

Recommended Posts

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.