i have a function what separate my file’s content. Example file:
001;"text1"
002;"text2"
003;"text3"
999
001;"tekst11"
Sometimes some indexes have a few values:
120;"text2"
120;"text3"
My function:
function parseCSV($file) {
$lines = file($file);
$output = array();
$i = -1;
foreach($lines as $line) {
$line = trim($line);
if($line == "999") {
$i++;
} else {
$ex = explode(";", $line, 2);
$val = str_replace("\"", "", $ex[1]);
//$dodaj = mysql_query("INSERT INTO `czynsz` (`$ex[$i]`) VALUES ('$val')");
if(isset($output[$i][$ex[0]])) {
if(is_array($output[$i][$ex[0]])) {
$output[$i][$ex[0]][] = $val;
} else {
$output[$i][$ex[0]] = array($output[$i][$ex[0]], $val);
}
} else {
$output[$i][$ex[0]] = $val;
}
$dodaj = mysql_query("INSERT INTO `czynsz` (`$ex[$i]`) VALUES ('$val')");
}
}
return $output;
}
$csvdata = parseCSV("woda.txt");
echo "<pre>\r\n";
print_r($csvdata);
echo "</pre>\r\n";
Above script makes me multi-level array tree and separate arrays from themselves by separator (999), because in my file are many arrays… When an index has many values – the script makes for him internal tree. For example:
Array
(
[0] => Array
(
[001] => 06-001-03-13
[002] => 06447
[003] => F
...
[097] =>
[120] => Array
(
[0] => text1
[1] => text2
)
)
[1] => Array
...
And there is my question:
I have a problem with save this mega array in my MySQL base. Every index in array have the same in data base. When the same index will have many values (ex. 120) it can be implode. Thank you with all my heart for any form of help… 🙁
Create a MySQL Table:
Then use the following php code to insert the data