Re: fgetcsv

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Danny Brow wrote:
Hi Everyone,

I'm trying to compare a value to the first field in a csv fILE (example
of the data below). Using while takes too long and I can't figure out
how to compare just one row at a time. I've tried some variations of the
following.

So are you trying to compare the first column or the first row? You've said you want to compare both.

To compare the first row:

<?php

$handle = fopen('file.csv', 'r') or die("unable to open file");

$my_row = array('1','2','John Smith');

$data = fgetcsv($handle, 1000, ",");

if ($data === false) {
	echo "Unable to get anything from the file.";
}

if (is_array($data)) {
	if ($data == $my_row) {
		echo "The first row matched\n";
	} else {
		echo "The first row didnt match\n";
	}
}

fclose($handle);


If you really do want to compare the first column, then the time to do it will be based on how big the csv file is. If you have a big file, it's going to take a long time to go through each row and then look at the first field.

<?php

$handle = fopen('file.csv', 'r') or die("unable to open file");

$my_value = '1';

$row_count = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	$row_count++;
	if ($data[0] == $my_value) {
		echo "Found my_value on row ", $row_count, "\n";
	} else {
		echo "Did not find my_value on row ", $row_count, "\n";
	}
}

fclose($handle);

--
Postgresql & php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux