Viktor Viktor - 1 year ago 82
PHP Question

PHP Arrays: Extract from CSV

Hi I'm using a simple import to CSV script that brings information into an array from a CSV file.

The sample output of the print_r($data) statement is:

Array ( [0] => Array ( [Email,Name,"Message Number","Date Added", ...etc ]) => email@email,com,myname,1001,"10/27/16 9:54pm EDT",... etc ) 1 => Array ( [Email,Name,"Message Number","Date Added",


shows Array ( [Email,Name,"Message Number","Date Added",

I'm trying to isolate the variables but each time I get a null result on the quoted values.

$importer = new CsvImporter($csv,true);
$data = $importer->get();
echo "<pre>";

$messageNumber = $data[$n]['Message Number'];

What am I doing wrong? It should return '1001' in that variable.

Note: I'm using the class CsvImporter on this page in PhP FgetFSV

Again, what I'm looking for is the definition to isolate an individual variable in an easy way.

Thank you!

Perhaps someone has a simpler way to read a CSV into a PHP Array than using the above class?

Answer Source

In the example given in the php docs for CsvImporter it's changing the default delimiter to \t. Try add ',' as the 3rd param for the class:

$importer = new CsvImporter($csv,true, ',');

Hope this helps!