[英]CSV league not skipping empty records
I am using PHPleague to parse csv and insert it to the database.我正在使用 PHPleague 解析 csv 并将其插入数据库。 https://csv.thephpleague.com/
https://csv.thephpleague.com/
And I have code as follows:我的代码如下:
$csv = Reader::createFromPath($path, 'r');
$csv->skipEmptyRecords();
$csv->setHeaderOffset(0);
$csv_header = $csv->getHeader();
$stmt = (new Statement())
->offset('0')
->limit('20')
;
$records = $stmt->process($csv);
foreach($records as $record){
print_r($record); // this show that it has processed empty records as well
}
exit();
I could trim the empty records myself as well as by calling following function as $records = $this->trimArray($records);
我可以自己修剪空记录,也可以通过调用以下 function as
$records = $this->trimArray($records);
public function trimArray($arr)
{
$final = array();
foreach($arr as $k => $v)
{
if(array_filter($v)) {
$final[] = $v;
}
}
return $final;
}
However, I want it to be trimmed before than that.但是,我希望在此之前对其进行修剪。 Instead of adding my own layer to filter it is there anyway on csvleague to
skipemptyrecords
.而不是添加我自己的层来过滤它无论如何都在 csvleague 到
skipemptyrecords
。 I have tried with $csv->skipEmptyRecords();
我试过
$csv->skipEmptyRecords();
but it is not skipping empty records.但它不会跳过空记录。 Am I doing anything wrong?
我做错什么了吗?
I want to skip all those empty records.我想跳过所有那些空记录。
I had a simple experience with League\CSV and tried to catch the data from a csv file.我对 League\CSV 有过简单的体验,并试图从 csv 文件中捕获数据。 The package codes depend strongly on the version that you have installed.
package 代码很大程度上取决于您安装的版本。 So at first step check the version of League.
所以第一步检查英雄联盟的版本。 I think you should read the League documentation page about stream filters.
我认为您应该阅读有关 stream 过滤器的联盟文档页面。 https://csv.thephpleague.com/9.0/connections/filters/
https://csv.thephpleague.com/9.0/connections/filters/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.