Php 限制已处理的csv行数
我有一个处理CSV并输出另一个CSV的小脚本 我想包括一个可选的限制器,以在所需的限制器停止处理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
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);
}