Flysystem/CSV filter a subset of columns

我与影子孤独终老i 提交于 2019-12-25 06:12:35

问题


Is it possible to filter a subset of columns of a CSV with Flysystem CSV?

I know that you can use AbstractCsv::addFilter(callable $callback) to add arbitrary filters, but I don't think you would use this to drop a column.


回答1:


I solved this by getting an array of the column positions that I wanted to filter out. Then, by setting up a new CSV object to write to e.g.

$new = Writer::createFromFileObject(new \SplTempFileObject);

With the CSV to read from, I used the each method to iterate through its rows, building a new row using the column indexes built earlier on and inserting into the new CSV. Remember to return true; in the each method or it won't iterate.

$csv = $csv->newReader();
$csv->setOffset(1);
$csv->each(function ($row) use ($new) {
    ... do stuff to alter the row ...
    $new->insertOne($alteredRow)
});


来源:https://stackoverflow.com/questions/36037953/flysystem-csv-filter-a-subset-of-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!