Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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 - Fatal编程技术网

Php 限制已处理的csv行数

Php 限制已处理的csv行数,php,Php,我有一个处理CSV并输出另一个CSV的小脚本 我想包括一个可选的限制器,以在所需的限制器停止处理CSV 我尝试了一个do-while和这个for-loop都不起作用 csv始终是每行而不是csv限制值 public function get_csv($csv_limit) { $file = fopen($this->csv_url, "r"); $rows = []; $idsColumnsWanted = array_flip(arr

我有一个处理CSV并输出另一个CSV的小脚本

我想包括一个可选的限制器,以在所需的限制器停止处理CSV

我尝试了一个
do-while
和这个
for-loop
都不起作用

csv始终是每行而不是csv限制值

  public function get_csv($csv_limit)
  {
    $file = fopen($this->csv_url, "r");
    $rows = [];
    $idsColumnsWanted = array_flip(array_keys($this->csv_headers));
    $skip_header = 0;
    
    for ($i = 0; $i <= $csv_limit; $i++)
    {
      while (false !== $fields = fgetcsv($file)) {
        if ($skip_header == 0) {
          $skip_header++;
          continue;
        }
        $this->csv_rows[] = array_intersect_key($fields, $idsColumnsWanted);
      }
    }

    fclose($file);
  }

for
循环中,继续使用
while
循环读取所有行

for ($i = 0; $i <= $csv_limit; $i++)
{
  while (false !== $fields = fgetcsv($file)) {  // This reads the entire file
  }
}
公共函数获取csv($csv\U限制)
{
$file=fopen($this->csv_url,“r”);
$rows=[];
$idsColumnsWanted=array_flip(array_键($this->csv_头));
对于($i=0;$i csv\u行[]=array\u intersect\u键($fields,$idsColumnsWanted);
}
fclose($文件);
}

感谢您的支持,我从您的片段中学到了很多,非常感谢。
for ($i = 0; $i <= $csv_limit; $i++)
{
  while (false !== $fields = fgetcsv($file)) {  // This reads the entire file
  }
}
$rowCount=0;
while ((false !== $fields = fgetcsv($file)) && $csv_limit > $rowCount++) {
    if ($skip_header == 0) {
        $skip_header++;
        continue;
    }
    $this->csv_rows[] = array_intersect_key($fields, $idsColumnsWanted);
}