Voting

: zero plus eight?
(Example: nine)

The Note You're Voting On

jc at goetc dot net
21 years ago
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;
}
}
?>

<< Back to user notes page

To Top