function createCsv($xml, $f, $parent = "") { foreach ($xml->children() as $item) { $hasChild = count($item->children()) > 0 ? true : false; if (!$hasChild) { $val = trim($item); $val = preg_replace("/\r\n|\r|\n/", '', $val); $val = preg_replace("/,/", '.', $val); $put_arr = array($parent . "_" . $item->getName(), $val); fputcsv($f, $put_arr, ',', '"'); } else { createCsv($item, $f, $parent . "_" . $item->getName()); } } }
require __DIR__ . '/config.php'; // BEGIN EXPORT PROCESS echo "# Export log\n\n"; $jsonFiles = glob(OUTPUT_FOLDER . '*'); foreach ($jsonFiles as $jsonFile) { $jsonFileName = pathinfo($jsonFile, PATHINFO_FILENAME); echo " * " . $jsonFileName . "\n"; $jsonIn = file_get_contents($jsonFile); $rows = json_decode($jsonIn, true, 512, JSON_BIGINT_AS_STRING); // export as JSON $jsonOut = json_encode($rows, JSON_OUTPUT_FLAGS); $jsonOut .= "\n"; file_put_contents(EXPORT_FOLDER . 'JSON/' . $jsonFileName . '.json', $jsonOut); echo " * JSON exported\n"; // export as CSV $csv = createCsv($rows); file_put_contents(EXPORT_FOLDER . 'CSV/' . $jsonFileName . '.csv', $csv); echo " * CSV exported\n"; // export as MySQL $mysql = createMysql($jsonFileName, $schemas['mysql'][$jsonFileName], $rows); $mysql .= "\n"; file_put_contents(EXPORT_FOLDER . 'MySQL/' . $jsonFileName . '.sql', $mysql); echo " * MySQL exported\n"; } // END EXPORT PROCESS function createCsv($data) { $firstElement = current($data); $columnNames = array_map('escapeForCsv', array_keys($firstElement)); $out = implode(',', $columnNames) . "\n"; foreach ($data as $columns) {