Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中遍历CSV文件_Php_Loops_Csv_While Loop_Counter - Fatal编程技术网

在PHP中遍历CSV文件

在PHP中遍历CSV文件,php,loops,csv,while-loop,counter,Php,Loops,Csv,While Loop,Counter,我有以下代码: <?php $handle = fopen("GeneratePicklist.csv", "r"); $data = fgetcsv($handle, 5000, ","); ?> 我只需要从每行中选择某些数据,然后转到下一行。如何使用while循环遍历到下一行?感谢您以后的回复。解析while循环后或解析while循环期间,您可以测试条件。(这是我写的一个类方法,所以忽略类的内容:)) 专用函数logicMakeCSVTable(){ $this->

我有以下代码:

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

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

解析while循环后或解析while循环期间,您可以测试条件。(这是我写的一个类方法,所以忽略类的内容:))

专用函数logicMakeCSVTable(){
$this->importData=Array();
$i=0;
而($row=fgetcsv($this->handle,999999,“,”)){
$num=计数($row);
对于($c=0;$c<$num;$c++){
$this->importData[$i][$c]=$row[$c];
}   
$i++;
}
$\会话['ImportData']=“”;
$\会话['ImportData']=$this->ImportData;
}

这通常是您处理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);

使用for循环或foreach循环代替while循环。或者将搜索移动到while循环中,不移动到选择所需数据的搜索位置。查看PHP,然后查看此部分:“我只需要从每行中选择某些数据,然后移动到下一行。”您可以在while循环中执行此操作。谢谢Alex。让我先研究一下,因为这对我来说已经很先进了:D哈哈。。如何将此应用于我的代码?您的CSV文件在while($row=fgetcsv($handle,999999,“,”)中循环。您要做的第一件事是询问数组的行数。然后在for语句循环中使用for语句遍历每一行。最终得到的是一个带有$importData[ROW][COLUMN]的二维数组,然后您可以调用(第1行第5列)为$importData[1][5]谢谢Alex!威尔:谢谢你,杰克!但是,如果我从CSV文件的开头开始遍历,这将起作用,但事实并非如此,因为CSV文件实际上是不同数据的集合,我只是尝试从它找到值
零件号的地方遍历,然后从那里开始。是否也可以使用您的代码执行此操作?@JudeJitsu您的意思是您只对从
零件号开始的列感兴趣?我已经编辑了我的答案
private function logicMakeCSVTable(){

    $this->importData = Array();
        $i = 0;
        while($row = fgetcsv($this->handle, 999999, ",")){

            $num = count($row);
            for($c=0; $c < $num; $c++){

                $this->importData[$i][$c] = $row[$c];
            }   

            $i++;
        }

        $_SESSION['ImportData']  = "";
        $_SESSION['ImportData'] = $this->importData;

}
$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);