note 43296 added to function.fgetcsv

From: Date: Thu, 17 Jun 2004 00:51:38 +0000
Subject: note 43296 added to function.fgetcsv
Groups: php.notes 
Request: Send a blank email to php-notes+get-71175@lists.php.net to get a copy of this message
I've had alot of projects recently dealing with csv files, so I created the following class to read a csv file and return an array of arrays with the column names as keys. The only requirement is that the 1st row contain the column headings. I only wrote it today, so I'll probably expand on it in the near future. class CSVparse { var $mappings = array(); function parse_file($filename) { $id = fopen($filename, "r"); //open the file $data = fgetcsv($id, filesize($filename)); /*This will get us the */ /*main column names */ if(!$this->mappings) $this->mappings = $data; while($data = fgetcsv($id, filesize($filename))) { if($data[0]) { foreach($data as $key => $value) $converted_data[$this->mappings[$key]] = addslashes($value); $table[] = $converted_data; /* put each line into */ } /* its own entry in */ } /* the $table array */ fclose($id); //close file return $table; } } ---- Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43296 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43296&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43296&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#71175) next »