Re: Need function to process tab delimited file using associative array

From: Date: Mon, 25 Oct 2004 13:26:40 +0000
Subject: Re: Need function to process tab delimited file using associative array
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-200245@lists.php.net to get a copy of this message
----- Original Message ----- From: "Justin French"> > The disadvantage is that you have to loop through the data twice (once > as a tab-delimited file, once as an assoc. array), which would have > obvious problems when there's a large set of data that needs to be > looped through often. > Justin French Justin, Your fgetcsv() function was what I was needing. I found this in it notes: <snip_from_fgetcsv> Quick and dirty script to take a csv file and turn it into a multidimensional associative array structure using the first line of the csv as the hash key names. <? $i = 0; $handle = fopen ("file.csv","r"); while($data = fgetcsv ($handle, 1000, ",")) { if ($i == 0) { $key_arr = $data; } reset($key_arr); while(list($index,$name) = each($key_arr)) { $temp_arr[$name] = $data[$index]; } $result_arr[$i] = $temp_arr; $i++; } fclose ($handle); ?> </snip_from_fgetcsv> Thanks! -eric wood

« previous php.general (#200245) next »