【问题标题】:how to insert csv array data in php mysql with respect to columns如何在 php mysql 中相对于列插入 csv 数组数据
【发布时间】:2017-06-30 16:06:04
【问题描述】:

我想将不同 CSV 文件中的数据导入 MySQL。 (不同的文件包含不同位置的必填列,但所有文件的标题值相同)

有些 CSV 文件是这样的;

-------------------------------------------------
|   Name, Father Name,  Contact, City, email,   |
-------------------------------------------------
|   Ali, Ahmed, 123456, isb. , ali@mail.com,    |
|   Fazi, Khan, 123456, hyd. , faiz@ymail.com,  |
-------------------------------------------------

有时 CSV 文件是这样的;

-------------------------------------------------
|   Name, Father Name,  Contact,  email, City,  |
-------------------------------------------------
|   Ali, Ahmed, 123456,  ali@mail.com,   isb. , |
|   Fazi, Khan, 123456,  faiz@ymail.com, hyd. , |
-------------------------------------------------

在探索和研究了许多建议后,我得到了这个解决方案。 但我不知道如何使用此代码在数据库中插入数组行。

$names      = array('Name', 'Father Name', 'contact', 'email');
$picked     = array();
$theData    = array();
$isFirstRow = true;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $numCols = count($data);
    $row     = array();


    // first row to select columns for extraction
    if($isFirstRow) {
        for($c=0; $c<$numCols; $c++)
            if(!in_array($data[$c], $names)
                continue;
            else
                $picked[] = $c;
        $isFirstRow = false;
    }

    // process remaining rows
    else {
        for($c=0; $c < $numCols; $c++)
            if(in_array($c, $picked))
                $row[] = $data[$c];
        $theData[] = $row;
    }

  }
fclose($handle);
}

请帮助我如何根据 $names[] 在 MySQL 中插入 $theData[] 的数据。 提前谢谢你:)

【问题讨论】:

  • 你考虑过在 MySQL 中使用LOAD DATA LOCAL INFILE 查询吗?
  • 看看this answer
  • @Barmar 感谢您的回复,我认为LOAD DATA LOCAL INFILE 无法加载特定列,如果可以,请告诉我。

标签: php mysql arrays csv


【解决方案1】:

我已经为此奋斗了很多次,我想出的最通用的解决方案是将您的已知字段“映射”到 CSV 中代表该字段的列。

以下是您的代码(修改后的),带有 cmets,用于解释该过程。

请注意,此例程要求 CSV 的第一行包含字段名称,并且它们与您所需的字段名称匹配。

$names      = array( 'Name', 'Father Name', 'contact', 'email' );
$picked     = array();
$theData    = array();
// new array to store the "mapping"
$map        = array();

$handle = fopen("test.csv", "r");
if ( FALSE !== $handle ) {
    // get the first row
    $row = fgetcsv( $handle, 1000, ',');

    // loop over desired fields, assign the column index to map array
    foreach( $names AS $name ) {
        // array_search will find the field name in the row array and return the index
        // note: this is a case-insensitive array-search
        // see this Q&A for more info: https://stackoverflow.com/a/4170079/870729
        $index = array_search( strtolower( $name ), array_map( 'strtolower', $row ) );
        if ( FALSE !== $index ) {
             $map[ $index ] = $name;
        }
    }

    // if not all fields present, error and exit
    if ( count( $map ) < count( $names ) ) {
         echo 'All fields must be present: ' . implode( ', ', $names );
         die();
    }

    while ( $data = fgetcsv($handle, 1000, "," ) ) {
        $row     = array();

        // loop over known fields / index and assign to record
        foreach( $map AS $index => $field ) {
             // $index is the column number / index
             // $field is the name of the field
             $row[ $field ] = $data[ $index ];
        }    

        $theData[] = $row;
    }

    fclose($handle);
}

现在,当您查看 $theData 时,它应该包含完整的“字段映射”,因此您可以根据这些字段名称插入每一行:

例子:

var_dump( $theData[0] );

会产生类似这样的结果:

array(
    'Name'        => 'Ali',
    'Contact'     => '123456',
    'Father Name' => 'Ahmed',
    'email'       => 'ali@mail.com'
    'City'        => 'isb.'
);

无论 CSV 中列的顺序如何。

【讨论】:

猜你喜欢
  • 2018-02-23
  • 1970-01-01
  • 2015-01-19
  • 2022-01-20
  • 2019-03-22
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多