php – 读取CSV到Array的特定列
作者:互联网
我试图读取我的csv文件中的某些数据并将其传输到数组.我想要的是获取某一列的所有数据,但我想从某一行开始(比方说,例如,第5行),有可能这样做吗?我现在只获取特定列中的所有数据,想要在第5行启动它,但想不出任何办法.希望你们能帮助我.谢谢!
<?php
//this is column C
$col = 2;
// open file
$file = fopen("example.csv","r");
while(! feof($file))
{
echo fgetcsv($file)[$col];
}
// close connection
fclose($file);
?>
解决方法:
是的,您可以定义一些标记来计算行.看看下面的解决方案.它将从第5行开始打印,您也可以通过索引访问colcol.例如.对于第二列,您可以使用$row [1]
$start_row = 5; //define start row
$i = 1; //define row count flag
$file = fopen("myfile.csv", "r");
while (($row = fgetcsv($file)) !== FALSE) {
if($i >= $start_row) {
print_r($row);
//do your stuff
}
$i++;
}
// close file
fclose($file);
标签:php,fgetcsv 来源: https://codeday.me/bug/20190608/1201209.html