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
  • Select an option

  • Save Kostanos/5639609 to your computer and use it in GitHub Desktop.

Select an option

Save Kostanos/5639609 to your computer and use it in GitHub Desktop.

Revisions

  1. Kostanos revised this gist May 24, 2013. 1 changed file with 0 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions csv-to-json.php
    Original file line number Diff line number Diff line change
    @@ -48,13 +48,6 @@ function csvToArray($file, $delimiter) {
    $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]);
  2. Kostanos revised this gist May 23, 2013. 1 changed file with 25 additions and 19 deletions.
    44 changes: 25 additions & 19 deletions csv-to-json.php
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,42 +1,48 @@
    #!/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
    $feed = 'https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Akse3y5kCOR8dEh6cWRYWDVlWmN0TEdfRkZ3dkkzdGc&single=true&gid=0&output=csv';
    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;
    }
    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);

    //Use first row for names
    $labels = array_shift($data);

    foreach ($labels as $label) {
    $keys[] = $label;
    @@ -48,14 +54,14 @@ function csvToArray($file, $delimiter) {
    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;
    }

    // Print it out as JSON
    echo json_encode($newArray);
    echo json_encode($newArray, JSON_PRETTY_PRINT);

    ?>
  3. @robflaherty robflaherty created this gist Sep 1, 2011.
    61 changes: 61 additions & 0 deletions csv-to-json.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    <?php
    /*
    * Converts CSV to JSON
    * Example uses Google Spreadsheet CSV feed
    * csvToArray function I think I found on php.net
    */

    header('Content-type: application/json');

    // Set your CSV feed
    $feed = 'https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Akse3y5kCOR8dEh6cWRYWDVlWmN0TEdfRkZ3dkkzdGc&single=true&gid=0&output=csv';

    // 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;
    }

    // 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;
    }

    // Print it out as JSON
    echo json_encode($newArray);

    ?>