需要获取大量信息,即
文件1:
10948|Book|Type1
文件2:
SHA512||0||10948
文件3:
0|10948|SHA512|c3884fbd7fc122b5273262b7a0398e63
我想把它变成这样
c3884fbd7fc122b5273262b7a0398e63|SHA512|Type1|Book
我无法访问实际的数据库,有什么办法可以做到这一点吗?基本上是寻找 $id = $file1[0]; if($file3[1] == $id)
或其他东西,除非更有效率。
每个 CSV 文件的行数在 100k-300k 之间。我不在乎是否需要一段时间,我可以让它在 EC2 上运行一段时间。
最佳答案
$data = array();
$fh = fopen('file1') or die("Unable to open file1");
while(list($id, $val1, $val2) = fgetcsv($fh, 0, '|')) {
$data[$id]['val1'] = $val1;
$data[$id]['val2'] = $val2;
}
fclose($fh);
$fh = fopen('file2') or die ("Unable to open file2");
while(list($method, null, null, null, $id) = fgetcsv($fh, 0, '|')) {
$data[$id]['method'] = $method;
}
fclose($fh);
$fh = fopen('file3') or die("Unable to open file3");
while(list(null, $id, null, $hash) = fgetcsv($fh, 0, '|')) {
$data[$id]['hash'] = $hash;
}
fclose($fh);
乏味,但你应该得到一个包含你想要的数据的数组。将其输出为另一个 csv 留给读者作为练习(提示:请参阅 fputcsv()
)。
关于php - 读取多个 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6413762/