以下是我一直在努力的示例表
表1
╔════╦══════════════╦═══════════╗════════════════════╗
║ id ║ file_id ║fieldname ║ from_column_number ║
╠════╬══════════════╬═══════════╣════════════════════╬
║ 1 ║ 5566533 ║ mid ║ 4 ║
║ 2 ║ 5566533 ║ old ║ 2 ║
║ 3 ║ 5566533 ║ fossil ║ 1 ║
║ 4 ║ 3232534 ║ old ║ 4 ║
║ 5 ║ 3232534 ║ mid ║ 3 ║
║ 6 ║ 3232534 ║ new ║ 2 ║
║ 7 ║ 3232534 ║ fossil ║ 1 ║
╚════╩══════════════╩═══════════╝════════════════════╝
我的 CSV文件在 file_id 列中包含了他们的ID,他们的标题名称及其出现在文件中的顺序列分别是 fieldname 和 from_column_number 。 即,在上述情况下,我已经显示了两个文件 file_id 5566533和 3232534。
他们的标题顺序为
5566533:
fossil old mid
3232534:
fossil new mid old
我需要将文件加载到下表中,使用上述信息重新排列为每个文件加载的方式
例如,在上述情况下,标识为5566533
的文件的字段mid
位于其第四位。
表2
╔════╦══════════════╦═══════════╗════════════════════╗
║ id ║ mid ║old ║ fossil ║
╠════╬══════════════╬═══════════╣════════════════════╬
║ 1 ║ 422342343 ║ 12222 ║ 342342 ║
║ 2 ║ 234234 ║ 43234 ║ 3243 ║
║ 3 ║ 345435355445 ║ 234234 ║ 32432 ║
║ 4 ║ 3455544534 ║ 2343245 ║ 4234 ║
║ 5 ║ 345345544454 ║ 432234 ║ 324545 ║
║ 6 ║ 554345345 ║ 34243 ║ 453242 ║
║ 7 ║ 77w345544533 ║ 34234455 ║ 5245345 ║
╚════╩══════════════╩═══════════╝════════════════════╝
为此,我只能使用普通的SQL和一些PHP。
答案 0 :(得分:1)
我想我弄清楚你在问什么。首先,您可以将字段映射表转换为数组。我已经手动完成了以下操作,但它应该演示您要执行的操作:
// Load some sample data regarding input file field ordering
// ---------------------------------------------------------
$csvinfo = array();
$csvinfo[5566533]['mid'] = 4;
$csvinfo[5566533]['old'] = 2;
$csvinfo[5566533]['fossil'] = 1;
$csvinfo[3232534]['old'] = 4;
$csvinfo[3232534]['mid'] = 3;
$csvinfo[3232534]['new'] = 2;
$csvinfo[3232534]['fossil'] = 1;
使用字段映射信息,您可以遍历文件列表,加载它们,然后使用字段映射信息提取您关心的数据:
// Process each of the file names loaded into csvinfo
// --------------------------------------------------
echo "Processing input files<br>\n";
foreach ($csvinfo as $fileid => $fldmap)
{
$raw = @file_get_contents("$fileid.csv");
if ( !strlen($raw) )
die("ERROR: Unable to load $fileid.csv contents!\n");
$data = explode("\n",$raw);
$i=0;
echo "<br>\n";
echo "Importing from $fileid.csv<br>\n";
foreach ($data as $line)
{
// Skip header line
// ----------------
if ( !$i++ || !strlen(trim($line)) )
continue;
$flds = explode(',',$line);
$midval = trim($flds[$fldmap['mid']-1]);
$oldval = trim($flds[$fldmap['old']-1]);
$fosval = trim($flds[$fldmap['fossil']-1]);
// Write fields to table here
// --------------------------
echo "... line " . ($i-1) . " $line with fossil $fosval mid $midval old $oldval<br>\n";
;
}
echo ($i-1) . " lines imported from $fileid.csv<br>\n";
}
上面的代码将生成以下调试信息:
Processing input files
Importing from 5566533.csv
... line 1 f812, o998, b234, m1892 with fossil f812 mid m1892 old o998
... line 2 f915, o123, b454, m9817 with fossil f915 mid m9817 old o123
2 lines imported from 5566533.csv
Importing from 3232534.csv
... line 1 f179, n723, m886, o912 with fossil f179 mid m886 old o912
... line 2 f791, n237, m868, o129 with fossil f791 mid m868 old o129
... line 3 f917, n372, m688, o291 with fossil f917 mid m688 old o291
3 lines imported from 3232534.csv
这是3232534.csv文件(使用垃圾数据手动创建):
fossil, new, mid, old
f179, n723, m886, o912
f791, n237, m868, o129
f917, n372, m688, o291
我不会建议代码特别强大或类似的东西。但是,如果我正确地解释了你的问题,这是一个起点。