Last active
September 4, 2015 09:44
-
-
Save Nyholm/dd6617e4cde9071a9eb2 to your computer and use it in GitHub Desktop.
Convert csv files to json files to import to Loco
This file contains hidden or 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
<?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