note 29561 deleted from function.fgetcsv by felipe

From: Date: Sun, 10 Feb 2008 13:33:29 +0000
Subject: note 29561 deleted from function.fgetcsv by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-135986@lists.php.net to get a copy of this message
Note Submitter: ---- Here's piece of code I wrote that reads the first line of a CSV file, creates an array based the field's name, and then reads the rest of the contents of the CSV file into the approate arrays. Great for quick and dirty access to a CSV file. <?php // Name of the file to read/ // $openFile is the only varible you will have change to for this to work. $openFile = "test.csv"; $row = 0; $handle = fopen ($openFile,"r"); while ($data = fgetcsv ($handle, 1000, ",")) { if($row == 0){ // set the varible array $num = count ($data); $csvNames = array(); for ($c=0; $c < $num; $c++) { $csvNames[$c] = $data[$c]; eval("$" . $data[$c] . " = array();"); } $row = 1; }else{ $num = count ($data); $row++; for ($c=0; $c < $num; $c++) { $buildEval = "$" . $csvNames[$c] . "[" . ($row - 1) . "] = \"" . $data[$c] . "\";"; eval($buildEval ); } } } fclose ($handle); ?>

« previous php.notes (#135986) next »