Crossing csv with fgetcsv

I have a csv file with three columns: email address , first name and last name . I have a scene where I can print an array using the following code:

<?php $file = fopen("testEmails.csv","r"); while(! feof($file)) { print_r(fgetcsv($file)); } fclose($file); ?> 

This prints an array, so each field in a row. What I want to print is purely the values ​​in the first column of the row. How this is done, the fgetcsv documentation seems very sketchy to me (relative newbie).

Thanks.

+8
php csv fgetcsv
source share
3 answers

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

http://php.net/manual/en/function.fgetcsv.php

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

fgetcsv returns a numeric index array representing the columns, so you just want to print the first column.

+16
source share

You are very close. You can simply print the first column since you already have an array.

But you can also use fgetcsv as a control variable for the loop.

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

Instead of print_r you can try echo

 <?php $file = fopen("testEmails.csv","r"); while(! feof($file)) { echo fgets($file). "<br />"; } fclose($file); ?> 
0
source share

All Articles