2

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?

7
  • 2
    Erh, looks like your extraction is wrong, that doesn't look like a proper array. Commented Oct 28, 2016 at 8:35
  • 1
    Could you show what print_r($data[0]) and print_r($data[1]) produces? Commented Oct 28, 2016 at 8:36
  • 1
    Array ( [Email,Name,"Message Number","Date Added", Commented Oct 28, 2016 at 8:38
  • 2
    @Viktor noooo, not as a comment - edit your original post instead. Commented Oct 28, 2016 at 8:38
  • 2
    Show us the part of your code where you extract the data into the array. Commented Oct 28, 2016 at 8:41

2 Answers 2

3

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!

Sign up to request clarification or add additional context in comments.

Comments

0
$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.