Created
December 16, 2015 00:04
-
-
Save vincent-peng/8ddc0dc77171dbeeb523 to your computer and use it in GitHub Desktop.
Convert .csv file to JSON
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
#!/usr/bin/php | |
<?php | |
/* | |
Usage: csv2json.php <path to .csv file> | |
*/ | |
$SCRIPT_PARENT_DIR = dirname($argv[0]); | |
$SOURCE_CSV_PATH = $argv[1]; | |
$SOURCE_JSON_PATH = dirname($SOURCE_CSV_PATH) . "/" . basename($SOURCE_CSV_PATH, ".csv") . ".json"; | |
if (empty($SOURCE_CSV_PATH)) die("The csv file name or URL is missed\n"); | |
// Arrays we'll use later | |
$keys = array(); | |
$newArray = array(); | |
// Function to convert CSV into associative array | |
function csvToArray($file, $delimiter) { | |
if (($handle = fopen($file, 'r')) !== FALSE) { | |
$i = 0; | |
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) { | |
for ($j = 0; $j < count($lineArray); $j++) { | |
$arr[$i][$j] = $lineArray[$j]; | |
} | |
$i++; | |
} | |
fclose($handle); | |
} | |
return $arr; | |
} | |
// Do it | |
$data = csvToArray($SOURCE_CSV_PATH, ','); | |
// Set number of elements (minus 1 because we shift off the first row) | |
$count = count($data) - 1; | |
// Use first row for names | |
$labels = array_shift($data); | |
foreach ($labels as $label) { | |
$keys[] = $label; | |
} | |
// Add Ids, just in case we want them later | |
$keys[] = 'id'; | |
for ($i = 0; $i < $count; $i++) { | |
$data[$i][] = $i; | |
} | |
// Bring it all together | |
for ($j = 0; $j < $count; $j++) { | |
$d = array_combine($keys, $data[$j]); | |
$newArray[$j] = $d; | |
} | |
// Write it out as JSON | |
// echo json_encode($newArray, JSON_PRETTY_PRINT); | |
$jsonFile = fopen($SOURCE_JSON_PATH, "w"); | |
fputs($jsonFile, json_encode($newArray, JSON_PRETTY_PRINT)); | |
fclose($jsonFile); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment