I have a csv file formatted as follows:
a;a1;1;0;1
b;b1;1;0;0
And using a php script below I can convert it to this:
a;a1;1;
a;a1;0;
a;a1;1;
b;b1;1;
b;b1;0;
b;b1;0;
Using this:
$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);
}
}
Where the output is an array:
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"
}
Now I'm trying to insert this array to a mysql table that I have set up, but I'm having trouble...
$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>";
Any ideas how to make it work?
'or"in your data(print_r)if that's what you are asking.