简体   繁体   中英

CSV league not skipping empty records

I am using PHPleague to parse csv and insert it to the database. 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);

    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 . I have tried with $csv->skipEmptyRecords(); but it is not skipping empty records. Am I doing anything wrong?

My file looks like: 在此处输入图像描述

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. The package codes depend strongly on the version that you have installed. So at first step check the version of League. I think you should read the League documentation page about stream filters. https://csv.thephpleague.com/9.0/connections/filters/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM