Skip to content

Instantly share code, notes, and snippets.

@Nyholm
Last active September 4, 2015 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nyholm/dd6617e4cde9071a9eb2 to your computer and use it in GitHub Desktop.
Save Nyholm/dd6617e4cde9071a9eb2 to your computer and use it in GitHub Desktop.
Convert csv files to json files to import to Loco
<?php
require 'vendor/autoload.php';
$dir = '/Users/tobias/path';
$finder = new \Symfony\Component\Finder\Finder();
$finder->files()->in($dir)->depth('== 0')->name('*.csv');
/** @var \Symfony\Component\Finder\SplFileInfo $file */
foreach ($finder as $file) {
$export = array();
$rows = explode("\n", $file->getContents());
foreach ($rows as $row) {
$arr = explode(';', $row);
if (count($arr) !== 2) {
continue;
} else {
list($key, $value) = $arr;
}
$export[trim($key)] = trim($value);
}
$exportFile = substr($file->getRealpath(), 0, -3).'json';
file_put_contents($exportFile, json_encode($export));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment