Skip to content

Instantly share code, notes, and snippets.

@yuxel
Created November 24, 2013 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuxel/7631353 to your computer and use it in GitHub Desktop.
Save yuxel/7631353 to your computer and use it in GitHub Desktop.
<?php
$row = 0;
$head = array();
$result = array();
$columnCount = 0;
$input = "/var/www/products.csv";
$output = "/tmp/outx.json";
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) {
exit('Kolon sayisi uymuyor');
}
$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";
@tunix
Copy link

tunix commented Nov 24, 2013

eyvallah reiz

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