gpt4 book ai didi

php - 如何在php mysql中相对于列插入csv数组数据

转载 作者:行者123 更新时间:2023-11-29 10:44:38 24 4
gpt4 key购买 nike

我想将不同 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[]的数据。 提前谢谢您:)

最佳答案

我已经多次与这个问题作斗争,我想出的最通用的解决方案是将您的已知字段“映射”到 CSV 中代表该字段的列。

下面是您的代码(已修改),带有注释,用于解释该过程。

请注意,此例程要求 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 中的列的顺序如何。

关于php - 如何在php mysql中相对于列插入csv数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44850744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com