jobo: csv-Datei auslesen und in Array speichern

Beitrag lesen

Hallo,

test.csv:

vorname,name,alter
bob,mayer,14
alina,mueller,18

  
<?php  
$fh = fopen("test.csv","r");  
$colNamesRead = false;  
$rowCount = 0;  
while($row = fgetcsv($fh)) {  
	if (!$colNamesRead) {  
		$colNames = $row;  
		$colNamesRead = true;  
	} else {  
		foreach ($row as $colNr => $cellValue) {  
			$table[$rowCount][$colNames[$colNr]] = $cellValue;  
		}	  
	}  
	$rowCount++;	  
}  
var_dump($table);  
  

gibt:

array(2) {
  [1]=>
  array(3) {
    ["vorname"]=>
    string(3) "bob"
    ["name"]=>
    string(5) "mayer"
    ["alter"]=>
    string(2) "14"
  }
  [2]=>
  array(3) {
    ["vorname"]=>
    string(5) "alina"
    ["name"]=>
    string(7) "mueller"
    ["alter"]=>
    string(2) "18"
  }
}
Gruß

jobo