How to create an array from a CSV file using PHP and the fgetcsv function How to create an array from a CSV file using PHP and the fgetcsv function php php

How to create an array from a CSV file using PHP and the fgetcsv function


Like you said in your title, fgetcsv is the way to go. It's pretty darn easy to use.

$file = fopen('myCSVFile.csv', 'r');while (($line = fgetcsv($file)) !== FALSE) {  //$line is an array of the csv elements  print_r($line);}fclose($file);

You'll want to put more error checking in there in case fopen() fails, but this works to read a CSV file line by line and parse the line into an array.


I think the str_getcsv() syntax is much cleaner, it also doesn't require the CSV to be stored in the file system.

$csv = str_getcsv(file_get_contents('myCSVFile.csv'));echo '<pre>';print_r($csv);echo '</pre>';

Or for a line by line solution:

$csv = array();$lines = file('myCSVFile.csv', FILE_IGNORE_NEW_LINES);foreach ($lines as $key => $value){    $csv[$key] = str_getcsv($value);}echo '<pre>';print_r($csv);echo '</pre>';

Or for a line by line solution with no str_getcsv():

$csv = array();$file = fopen('myCSVFile.csv', 'r');while (($result = fgetcsv($file)) !== false){    $csv[] = $result;}fclose($file);echo '<pre>';print_r($csv);echo '</pre>';


I have created a function which will convert a csv string to an array. The function knows how to escape special characters, and it works with or without enclosure chars.

$dataArray = csvstring_to_array( file_get_contents('Address.csv'));

I tried it with your csv sample and it works as expected!

function csvstring_to_array($string, $separatorChar = ',', $enclosureChar = '"', $newlineChar = "\n") {    // @author: Klemen Nagode    $array = array();    $size = strlen($string);    $columnIndex = 0;    $rowIndex = 0;    $fieldValue="";    $isEnclosured = false;    for($i=0; $i<$size;$i++) {        $char = $string{$i};        $addChar = "";        if($isEnclosured) {            if($char==$enclosureChar) {                if($i+1<$size && $string{$i+1}==$enclosureChar){                    // escaped char                    $addChar=$char;                    $i++; // dont check next char                }else{                    $isEnclosured = false;                }            }else {                $addChar=$char;            }        }else {            if($char==$enclosureChar) {                $isEnclosured = true;            }else {                if($char==$separatorChar) {                    $array[$rowIndex][$columnIndex] = $fieldValue;                    $fieldValue="";                    $columnIndex++;                }elseif($char==$newlineChar) {                    echo $char;                    $array[$rowIndex][$columnIndex] = $fieldValue;                    $fieldValue="";                    $columnIndex=0;                    $rowIndex++;                }else {                    $addChar=$char;                }            }        }        if($addChar!=""){            $fieldValue.=$addChar;        }    }    if($fieldValue) { // save last field        $array[$rowIndex][$columnIndex] = $fieldValue;    }    return $array;}