-
-
Save anoriar/bbc67a988a2206a0cb1f383ad2a71507 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCsvData($fileName) | |
{ | |
$fp = fopen($fileName, 'r'); | |
$head = fgetcsv($fp, 4096, ';', '"'); | |
$res = array(); | |
$counter = 1; | |
while ($column = fgetcsv($fp, 0, ';', '"')) { | |
$counter++; | |
$column = array_combine($head, $column); | |
$res[] = $column; | |
} | |
fclose($fp); | |
return $res; | |
} | |
function putCsvData($fileName, $data) | |
{ | |
$fp = fopen($fileName, 'rw'); | |
$head = fgetcsv($fp, 4096, ';', '"'); | |
if (!$head) { | |
$headStr = implode(";", array_keys($data)); | |
file_put_contents($fileName, $headStr); | |
$head = array_keys($data); | |
} | |
if ($head) { | |
foreach ($head as $code) { | |
if ($data[$code]) { | |
$csvData[] = $data[$code]; | |
} | |
} | |
} | |
if (count($head) == count($csvData)) { | |
$csvStr = implode(";", $csvData); | |
} | |
if ($csvStr) { | |
file_put_contents($fileName, file_get_contents($fileName) . "\r\n" . $csvStr); | |
} | |
fclose($fp); | |
} | |
getCsvData("../orders.csv"); | |
putCsvData("../orders.csv", ["THEME" => "Paola1", "NAME" => "name2", "TEL" => "tel2", "MAIL" => "mail1", "IP" => "ip1", "POLICY" => "no", "DATE" => "date1"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment