我有一个格式如下的 csv 文件:
a;a1;1;0;1
b;b1;1;0;0
并使用下面的 php 脚本,我可以将其转换为:
a;a1;1;
a;a1;0;
a;a1;1;
b;b1;1;
b;b1;0;
b;b1;0;
使用这个:
$data = array();
foreach($lines as $value) {
$value = explode(";", $value);
$first = array_splice($value, 0, 2);
foreach($value as $x){
$row = $first;
$row[] = $x;
$data[] = implode(';', $row);
}
}
其中输出是一个数组:
array(6) {
[0]=>
string(6) "a;a1;1"
[1]=>
string(6) "a;a1;0"
[2]=>
string(6) "a;a1;1"
[3]=>
string(6) "b;b1;1"
[4]=>
string(6) "b;b1;0"
[5]=>
string(6) "b;b1;0"
}
现在我正在尝试将此数组插入到我已设置的 mysql 表中,但我遇到了麻烦......
$file = "_ss.csv";
$lines = file($file);
$count = count($lines);
$data = array();
$i = 0;
foreach($lines as $value){
$value = explode(";", $value);
$first = array_splice($value, 0, 2);
foreach($value as $x){
$row = $first;
$row[] = $x;
$data[] = implode(',', $row);
$dump = implode(',', $data);
$query="INSERT INTO csv_test2 VALUES ('$dump')";
$init=mysql_query($query);
$i++;
echo $dump;
}
}
echo "<pre>";
print_r($dump);
echo "</pre>";
任何想法如何使它工作?