note 43296 modified in function.fgetcsv by aidan

From: Date: Thu, 01 Jul 2004 00:58:32 +0000
Subject: note 43296 modified in function.fgetcsv by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72205@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. <?php 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; } } ?> --was-- 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; } } http://php.net/manual/en/function.fgetcsv.php

« previous php.notes (#72205) next »