php - 在 PHP 中遍历 CSV 文件

标签 php loops csv while-loop counter

我有这段代码:

<?php
   $handle = fopen("GeneratePicklist.csv", "r");
   $data = fgetcsv($handle, 5000, ",");
?>

这会打开一个 php 文件并将其转换为 php 数组。然后我将列出数组内容并将它们显示到格式化页面中。我有这段代码,它正在运行。

<?php
    echo "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
        <tr>
            <td class=\"dataHeader\" width=\"100\">Mat.Loc.</td>
            <td class=\"dataHeader\" width=\"20\">Qty</td>
            <td class=\"dataHeader\">Description</td>
            <td class=\"dataHeader\" width=\"150\">Part Number</td>
            <td class=\"dataHeader\" width=\"30\">Multiplier</td>
            <td class=\"dataHeader\" width=\"80\">Total Qty</td>
        </tr>
";
$locatePartNoString = array_search("Part Number",$data);
$arrayStart = $locatePartNoString-1;
end($data);
$lastField = key($data);
$counter = ($lastField - $arrayStart);
$arrayBegin = $locatePartNoString+6;
while($counter>0) {
    echo "
        <tr>
            <td class=\"centered\"><span class=\"title\">*".$data[$arrayBegin+4]."*</span><br>".$data[$arrayBegin+4]."</td>
            <td id=\"qty".$counter."\" class=\"centered\">".$data[$arrayBegin+1]."</td>
            <td>".$data[$arrayBegin+2]."</td>
            <td class=\"centered\">".$data[$arrayBegin]."</td>
            <td class=\"centered\"><input type=\"text\" id=\"multiplier".$counter."\" name=\"multiplier".$counter."\" value=\"1\" size=\"3\" style=\"border:1px dotted #CCC;\"></td>
            <td class=\"centered\"><input type=\"text\" id=\"total".$counter."\" name=\"total".$counter."\" value=\"1\" size=\"3\" style=\"border:1px dotted #CCC;\"></td>
        </tr>

    ";
    $counter--;
}
echo "</table>";
?>

我想做的是遍历 CSV 文件并仅在 $arrayBegin 变量和我的 CSV 数据的最后一部分覆盖范围内选择某些数据。我已经完成了第一行并且它工作正常但我不知道如何递增所以我的指针移动到 CSV 文件的下一行并执行与我的第一行数据相同的公式。

这是 CSV 内容的示例,有 7 个字段:

ÊÊ,Part Number,Qty,Description,BB Type,Material Location,Serial Number
ÊÊ,013224-001,1,"PCA,DDR2-800,MINIDIMM MOD256MBx 40",BOM,ASSY,ID121608ZS
ÊÊ,122657-00A,1,SOFTWARE TEST (US M3 CTO),BOM,ASSY,
ÊÊ,376383-002,8,"ASSY, BLANK,SFF",BOM,ASSY,
ÊÊ,458943-003,1,"CA ASSY, SFP BATTERY, 15 POS, 28AWG, 24",BOM,ASSY,
ÊÊ,460499-001,1,"ASSY, 4/V650HT BATTERY CHARGER MODULE",BOM,ASSY,
ÊÊ,499256-001,2,"ASSY, BLANK,MEDIA BAY,ML350G6",BOM,ASSY,
ÊÊ,500203-061,2,"DIMM,4GB PC3-10600R,256Mx4,RoHS",BOM,ASSY,RAKWF8DXV2Q100

我只需要从每一行中选择某些数据,然后转到下一行。我如何使用我的 while 循环遍历到下一行?感谢您日后的回复。

最佳答案

这通常是您浏览 CSV 文件的方式,从您已有的开始:

$handle = fopen("GeneratePicklist.csv", "r");
$firstRow = fgetcsv($handle, 5000, ",");
$partNumberPosition = array_search("Part Number", $firstRow, true);

此时您已经阅读了第一行,它通常包含字段名称。您还确定了“部件号”位于哪一列。

然后获取其余部分:

// the function `fgetcsv()` will return `false` when the end-of-file is reached
while (false !== ($row = fgetcsv($handle, 5000))) {
    // we have read a(nother) row of data
    // if you're not interested in the columns before the 'Part Number'
    // you can slice them off
    $row = array_slice($row, $partNumberPosition);
    // at this point, $row contains:
    // 0 => the part number
    // 1 => the quantity
    // etc...
}
// we're done, close the file
fclose($handle);

关于php - 在 PHP 中遍历 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10924674/

相关文章:

VB.NET - 如何在 For Each 循环中移动到下一个项目?

macos - 单列 CSV 文件是否有逗号?

java - 在 java 中使用 ArrayList 的 getter 和 setter

java - 如何修复按重量对包裹进行排序的循环

php - 在 php 代码中执行 Gstreamer 管道脚本

php - 使用 .p8 文件使用 php 代码发送 ios 推送通知

php - 违反完整性约束 : 1048 Column 'postqs_id' cannot be null

javascript - 循环执行 Grunt 任务

PHP - 每年更新数据。 -工艺建议-

PHP:在带有 MYSQL 的 MDB2 中的字符串上使用 quote()