Flysystem/CSV过滤列的子集 [英] Flysystem/CSV filter a subset of columns

查看:123
本文介绍了Flysystem/CSV过滤列的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 Flysystem CSV 过滤CSV列的子集?

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

我知道您可以使用AbstractCsv::addFilter(callable $callback)添加任意过滤器,但是我认为您不会使用它来删除列.

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.

推荐答案

我通过获取要过滤掉的列位置数组解决了这一问题.然后,通过设置一个新的CSV对象来写入例如

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);

要读取CSV,我使用了each方法来遍历其行,使用先前构建的列索引来构建新行,然后插入到新CSV中.记住要使用each方法中的return true;,否则它不会迭代.

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)
});

这篇关于Flysystem/CSV过滤列的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆