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:
我有一个包含3列的csv文件:电子邮件地址,名字和姓氏。我有一个阶段,我可以使用以下代码打印出数组:
<?php
$file = fopen("testEmails.csv","r");
while(! feof($file))
{
print_r(fgetcsv($file));
}
fclose($file);
?>
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).
这将打印数组,因此每行都有一个字段。我想要它打印的纯粹是行的第一列中的值。如何做到这一点,关于fgetcsv的文档对我来说似乎非常粗略(相对初学者)。
Thanks.
谢谢。
3 个解决方案
#1
14
The first example in the fgetcsv
documentation contains the nuggets of what you need.
fgetcsv文档中的第一个示例包含您需要的块。
http://php.net/manual/en/function.fgetcsv.php
http://php.net/manual/en/function.fgetcsv.php
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.
fgetcsv返回一个表示列的数字索引数组,因此您只想打印第一列。
#2
1
You're quite close. You already have the array, so you could just print the first column.
你很亲密您已经拥有该阵列,因此您只需打印第一列即可。
But, you could also use fgetcsv
itself as the control variable for the loop.
但是,您也可以使用fgetcsv本身作为循环的控制变量。
while (($array = fgetcsv($file) !== FALSE) {
print_r($array[0]);
}
#3
0
Instead of print_r
you may try echo
您可以尝试使用echo来代替print_r
<?php
$file = fopen("testEmails.csv","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
#1
14
The first example in the fgetcsv
documentation contains the nuggets of what you need.
fgetcsv文档中的第一个示例包含您需要的块。
http://php.net/manual/en/function.fgetcsv.php
http://php.net/manual/en/function.fgetcsv.php
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.
fgetcsv返回一个表示列的数字索引数组,因此您只想打印第一列。
#2
1
You're quite close. You already have the array, so you could just print the first column.
你很亲密您已经拥有该阵列,因此您只需打印第一列即可。
But, you could also use fgetcsv
itself as the control variable for the loop.
但是,您也可以使用fgetcsv本身作为循环的控制变量。
while (($array = fgetcsv($file) !== FALSE) {
print_r($array[0]);
}
#3
0
Instead of print_r
you may try echo
您可以尝试使用echo来代替print_r
<?php
$file = fopen("testEmails.csv","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>