On Wed, January 9, 2008 4:35 pm, Danny Brow wrote: > 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. > > > //Common for all trials > $demoID = fopen("newDemoID.csv", "r");; > $ID = "43"; > > $data = fgetcsv($demoID); > > > > First try with while: > > /* > while ($data) { > if ($data[0] == $wolfID) { > print $data[1] . "," . $data[2] . "," . $data[3] . > ".\n"; > } > } > */ > > > Takes for every. *IF* the line you are looking for is almost always in the early rows, and *IF* you only need the first match, you can use the above and put a "break;" inside the if { } This will let the script end as soon as it finds the answer. You do have another $data = fgetcsv($demoID); inside the while loop, right? Otherwise you are looking at the same first line over and over and over and over and over until PHP gives up on you. > I can't use just the below because it only compares the first row. > > /* > if ($data[0] == $wolfID) { > print $data[1] . "," . $data[2] . "," . $data[3] . ".\n"; > } > > */ > > > I know this is simple, but I've hit codes block due to lack of sleep. It's actually not simple at all, since you are trying to scan through a large file (it must be large to take a long time) and find only one chunk at the beginning of a row. There are several other techniques to consider: #1. Put the data into an SQL database before you try to search for specific field/value information. SQL was designed to make this stuff fast. CSV was designed to dump files out to pass between spreadsheets/databases. #2. *IF* the file isn't TOO large compared to your available RAM, you could load the whole thing with "file_get_contents" and then do some preg magic to find the line you want: $file = file_get_contents("/full/path/to/file"); preg_match("/^$wolfID,.*\$/ms", $file, $match); var_dump($match); //use preg_match_all to find ALL the lines that match #3. *IF* the file is too large, and *IF* the CSV parsing is the slow part (which I doubt) it's possible that a simple fgets() and simple check sch as: if (substr($line, 0, strlen($wolfID)) == $wolfID) would be faster... Actually, pull the strlen($wolfID) out of the loop into a variable, and maybe use strpos instead of substr instead and... The biggest time sink is probably reading the file line by line, though, not the actual processing which is pretty minimal... In which case this solution will probably not significantly beat your current time. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php