note 29561 added to function.fgetcsv

From: Date: Mon, 17 Feb 2003 17:23:56 +0000
Subject: note 29561 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-44105@lists.php.net to get a copy of this message
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); ?> -- http://www.php.net/manual/en/function.fgetcsv.php http://master.php.net/manage/user-notes.php?action=edit+29561 http://master.php.net/manage/user-notes.php?action=delete+29561 http://master.php.net/manage/user-notes.php?action=reject+29561

« previous php.notes (#44105) next »