Larry Dayhoff
Larry Dayhoff

Reputation: 1

Why does fgetcsv not return the array?

There is something happening in a while loop for fgetcsv. I have never seen before.
Has anyone else seen it? I have written code before to extract data from a csv file and have never had this problem. When I do the extract with individual call the data is extracted just fine. I have included my examples. This is my little test fill: First Name,Last Name Ann,Bessire Kimberly,Biggerstaff Mary,Brice

When I do this:

$col_data = fgetcsv($csvfile);
print_r($col_data);
$col_data = fgetcsv($csvfile);
print_r($col_data);
$col_data = fgetcsv($csvfile);
print_r($col_data);
$col_data = fgetcsv($csvfile);
print_r($col_data);

The output looks like this;

Array ( [0] => First Name [1] => Last Name )
Array ( [0] => Ann [1] => Bessire )
Array ( [0] => Kimberly [1] => Biggerstaff )
Array ( [0] => Mary [1] => Brice )

If I do this:

while ($col_data = fgetcsv($csvfile) !== false){
    print_r($col_data);
}

The output looks like this:

1111111111111111111111111111111111

I am baffled on what is going on.

Upvotes: 0

Views: 40

Answers (1)

Ron
Ron

Reputation: 6591

fixing your while loop:

while (($col_data = fgetcsv($csvfile)) !== false){
    print_r($col_data);
}

Upvotes: 0

Related Questions