Viktor
Viktor

Reputation: 547

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",

print_r($data[0])

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.

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

$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?

Upvotes: 2

Views: 93

Answers (2)

Rwd
Rwd

Reputation: 35220

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!

Upvotes: 3

Viktor
Viktor

Reputation: 547

$csv="members.csv";
$importer = new CsvImporter($csv,true, ',');
$data = $importer->get();
echo "<pre>";
$n=0;
$messageNumber = $data[$n]['Message Number']; 
print $messageNumber;

Returns the correct '1001' as expected. The solution is marked above.Thanks to Ross. Simply changing the $importer = new CsvImporter($csv,true, ','); solved the entire puzzle.

Upvotes: 0

Related Questions