使用fgetcsv遍历csv [英] Looping through a csv with fgetcsv
问题描述
我有一个包含三列的csv文件:电子邮件地址,名字和姓氏.我已经可以使用以下代码打印出数组了:
I have a csv file with 3 columns: email address, first name and last name. I have got the stage where I can print out the array using the following code:
<?php
$file = fopen("testEmails.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
这将打印数组,因此一行中的每个字段.我要打印的只是该行第一列中的值.如何做到这一点,关于fgetcsv的文档对我(一个相对的初学者)来说似乎很粗略.
This prints the array, so every field in a row. What I want it to print is purely the values in the first column of the row. How would this be done, documentation on fgetcsv seems very sketchy to me (a relative beginner).
谢谢.
推荐答案
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()
返回表示列的数字索引数组,因此您只想打印第一列.
fgetcsv()
returns a numerically indexed array of representing the columns, so you just want to print the first column.
这篇关于使用fgetcsv遍历csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!