Convert CSV data to array?

I want to take CSV data already read in to memory and convert it to an array using the column names as the variable names (or index) to access the columns. The first line of the CSV file has the column names, all the others are data. How do I do that are traverse the data??

TIA!!

This is how i did the samething, only i just wanted the 2nd and 3rd lines (in this version, the file is manually uploaded)
[php]$file = $path.$_FILES[‘file’][‘name’];
$fcontents = file($file, FILE_IGNORE_NEW_LINES);
for($i = 1; $i < 2; $i++) {

$line = $fcontents[$i];
$ar = explode("\t", $line);											
foreach($ar as $key => $value) {
	$ar[$key] = trim(mysql_real_escape_string($value));
}
$dt[] = $ar[1]; // line 1
$dt[] = $ar[2]; // line 2

}[/php]

Just change 1 to 0 and the 2 to 1, that should just get the first line.

Thanks - I ended up finding this one:

function csv_to_array($input, $delimiter=’,’)
{
$header = null;
$data = array();

$csvData = str_getcsv(str_replace("\r\n", "\n", $input), "\n", "");

foreach($csvData as $csvLine){
	if(is_null($header)) $header = explode($delimiter, $csvLine);
	else {
		$items = explode($delimiter, $csvLine);
		for($n = 0, $m = count($header); $n < $m; $n++){
			 $prepareData[$header[$n]] = $items[$n];
		}
		$data[] = $prepareData;
	}
}
return $data;

}

Sponsor our Newsletter | Privacy Policy | Terms of Service