遍历PHP中的CSV文件

Jud*_*tsu 2 php csv counter loops while-loop

我有这个代码:

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

这会打开一个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>";
?>
Run Code Online (Sandbox Code Playgroud)

我要做的是遍历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
Run Code Online (Sandbox Code Playgroud)

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

Ja͢*_*͢ck 7

这通常是您通过CSV文件的方式,从您已经拥有的文件开始:

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

此时您已经读取了第一行,该行通常包含字段名称.您还确定了"部件号"所在的列.

并取其余:

// 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);
Run Code Online (Sandbox Code Playgroud)