Re: fgetcsv

From: Date: Thu, 10 Jan 2008 00:55:47 +0000
Subject: Re: fgetcsv
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-266985@lists.php.net to get a copy of this message
Danny Brow wrote:
I need to compare the first field of each row. But this idea is shot to hell, i've been running one of the examples on the file and it's been about an hour+ already... 6500 records have to be checked... I think MySQL is calling my name right now. Thanks, Dan On Thu, 2008-01-10 at 09:59 +1100, Chris wrote:
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/
even on a slow machine 6500 records should be very fast. Are you doing this through a web interface or command line? If browser, are you sure that your script is still running? Could the PHP timeout be exceeded? Try the other example that I gave you. <pre><?php error_reporting(E_ALL); ini_set('display_errors', 1); $st = microtime(true); $file = '/tmp/testing.csv'; $fh = fopen($file, "w+") or die('Could not open new file for writing'); for ( $i=0; $i<10000; $i++ ) { fputcsv($fh, array($i,1,"Smith{$i}","Myrtle{$i}")); } fclose($fh); //Common for all trials $fh = fopen($file, "r") or die('Could not open file ({$file}) for reading'); # What we are looking for $ID = "9999"; # Loop through file handler # if we get data, we check it # if/when we get false, we break out of the while loop while ( ($data = fgetcsv($fh) ) !== false ) {
        # Check for the value you are looking for.
if ( $data[0] == $ID ) { # Obvious... echo "{$data[0]},{$data[1]},{$data[2]},{$data[3]}\n"; } } fclose($fh); echo "Run time was ".round(microtime(true) - $st, 6)." seconds."; ?> I looked for the last result, time for me was 9999,1,Smith9999,Myrtle9999 Run time was 0.333304 seconds. -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#266985) next »