Skip to content

Instantly share code, notes, and snippets.

@Kostanos
Forked from robflaherty/csv-to-json.php
Last active December 17, 2015 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Kostanos/5639609 to your computer and use it in GitHub Desktop.
Save Kostanos/5639609 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
/*
* Converts CSV to JSON
* Example uses Google Spreadsheet CSV feed
* csvToArray function I think I found on php.net
*/
/*
* Using of script in command line:
* ./csv-to-json.php csv.file.name.or.url > json.file.name
*/
header('Content-type: application/json');
// Set your CSV feed
if (empty($argv[1])) die("The csv file name or URL is missed\n");
$feed = $argv[1];
// 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($feed, ',');
// 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;
}
// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$newArray[$j] = $d;
}
// Print it out as JSON
echo json_encode($newArray, JSON_PRETTY_PRINT);
?>
@Kostanos
Copy link
Author

This version is for command line use

@adamrights
Copy link

Thanks man I like it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment