Looping through a csv with fgetcsv

The first example in the fgetcsv() documentation contains the nuggets of what you need.

$file = fopen("testEmails.csv","r");

while (($data = fgetcsv($file)) !== FALSE)
{
    echo "email address " . $data[0];
}

fgetcsv() returns a numerically indexed array of representing the columns, so you just want to print the first column.


Instead of print_r you may try echo

<?php
$file = fopen("testEmails.csv","r");

while(! feof($file))
{
echo fgets($file). "<br />";
}

fclose($file);
?>

You're quite close. You could just print the first column since you already have the array.

But, you could also use fgetcsv itself as the control variable for the loop.

while (($array = fgetcsv($file)) !== FALSE) {
      print_r($array[0]);
}

Tags:

Php

Csv

Fgetcsv