Php 如何使用CSV上传插入一些常用数据?

Php 如何使用CSV上传插入一些常用数据?,php,mysql,wordpress,csv,Php,Mysql,Wordpress,Csv,使用CSV上传功能,我可以将所有重复数据上传到MySQL数据库。假设我有一些字段,如: +------+-------+-----+--------+ | Year | Month | Day | Figure | +------+-------+-----+--------+ 2012 01 01 200 2012 01 02 303 2012 01 03 632 ... 2012 01

使用CSV上传功能,我可以将所有重复数据上传到MySQL数据库。假设我有一些字段,如:

+------+-------+-----+--------+ | Year | Month | Day | Figure | +------+-------+-----+--------+ 2012 01 01 200 2012 01 02 303 2012 01 03 632 ... 2012 01 31 323 +------+-------+-----+--------+ 年和月字段中的纯重复。如果我们可以在CSV中以任何方式使这两个字段通用,那么对输入用户来说就更好了

因此,底线是:

我们如何使用CSV上传以较少的输入插入一些公共字段?
正如@DevZer0所建议的,CSV不是空字段或缺少字段的好选择

如果您想使用基于文本的解决方案,可以尝试JSON/XML

如果您想使用托管方法,可以尝试使用DBMS,例如MySQL/MSSQL


或者是上面提到的,SQLite,一个基于文件的数据库系统。

我真的不知道这将如何与您提到的wordpress插件集成,但要采用一种技术,即使用前一条记录的值填充空白列,您可以这样做,前提是csv文件中有一个具有唯一字段名的标题行

<?php
$fp = fopen($csvFile, "r");
// get the header line

$header = fgetcsv($fp);
// seed the previous record array
$previousRec = array_combine($header, array_fill(0, count($header), ""));

while ($row = fgetcsv($fp)){
    $row = array_combine($header, $row);
    // filter with a closure requires php 5.3
    $filtered = array_filter($row, function($a){ return trim($a) !== "";});

    // merge the new data on top of the old with filtered values gone.
    $row = array_merge($previousRec, $filtered);

    // do something with row

    // store the row as the previousRec for the next time arround.
    $previousRec = $row;
} 

为什么都是大写和加粗?没什么不寻常的这只是我的一种风格。强调主要问题:我们最好谈谈这个话题。换一种不同的数据格式。喜欢xml@MikeW:你没有理解基本问题。请修改一下。我想要一个重复数据的解决方案-给定的场景只是一个示例:@我想你没有理解我的意思。
<?php
$fp = fopen($csvFile, "r");
// get the header line

$header = fgetcsv($fp);
// seed the previous record array
$previousRec = array_combine($header, array_fill(0, count($header), ""));

while ($row = fgetcsv($fp)){
    $row = array_combine($header, $row);
    // filter with a closure requires php 5.3
    $filtered = array_filter($row, function($a){ return trim($a) !== "";});

    // merge the new data on top of the old with filtered values gone.
    $row = array_merge($previousRec, $filtered);

    // do something with row

    // store the row as the previousRec for the next time arround.
    $previousRec = $row;
}