Skip to content

Instantly share code, notes, and snippets.

@tunix
Created January 24, 2014 23:04
Show Gist options
  • Save tunix/8608648 to your computer and use it in GitHub Desktop.
Save tunix/8608648 to your computer and use it in GitHub Desktop.
<?php
$row = 0;
$head = array();
$result = array();
$columnCount = 0;
$input = @$argv[1];
$output = @$argv[2];
if (empty($input) OR empty($output)) {
die("Either input or output is empty!");
}
if (($handle = fopen($input, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000000, "|")) !== FALSE) {
$num = count($data);
if ($row == 0) {
$head = $data;
$columnCount = $num;
$row++;
continue;
}
if ($num != $columnCount) {
echo 'Kolon sayisi uymuyor (num:' . $num . ', columnCount: ' . $columnCount . ')';
die(print_r($data, true));
}
$row++;
$obj = new StdClass();
for ($c=0; $c < $num; $c++) {
$h = $head[$c];
$obj->$h = $data[$c];
}
$result[] = $obj;
}
fclose($handle);
}
file_put_contents($output, json_encode($result, JSON_PRETTY_PRINT));
echo "$output olusturuldu\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment