我正在尝试导入CSV文件.由于我们使用的程序,第一行基本上是我想要跳过的所有标题,因为我已经将自己的标题放在via HTML中.如何让代码跳过CSV的第一行?(strpos命令用于切断所有行中的第一个字段.)
<?php
$row = 1;
if (($handle = fopen("ptt.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if(strpos($data[$c], 'Finished') !== false) {
$c++;
echo "<TR> <TD nowrap>" . $data[$c] . "</ TD>"; }
Else{
echo "<TD nowrap>" . $data[$c] . "</ TD>";
}
}
}
fclose($handle);
}
?>
Run Code Online (Sandbox Code Playgroud)
Rah*_*pta 55
Rather than using if condition for checking whether it is the first row, a better solution is to just add an extra line of code before the line from where the while loop starts as shown below :
....
.....
fgetcsv($handle);//Adding this line will skip the reading of th first line from the csv file and the reading process will begin from the second line onwards
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
.......
.......
Run Code Online (Sandbox Code Playgroud)
It is just as simple......
Joh*_*nce 30
无论如何,当您跟踪行号时,您可以使用continue跳过第一行的其余循环.
例如,在while循环的开头添加它(就在上面$num = count($data)):
if($row == 1){ $row++; continue; }
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以做到这一点,但只要确保当你继续,$row仍在增加或你将获得无限循环!
| 归档时间: |
|
| 查看次数: |
63766 次 |
| 最近记录: |