Skip to content

Instantly share code, notes, and snippets.

@yunchih
Last active August 29, 2015 14:06
Show Gist options
  • Save yunchih/52797a5bdd1d70362ef7 to your computer and use it in GitHub Desktop.
Save yunchih/52797a5bdd1d70362ef7 to your computer and use it in GitHub Desktop.
<?php
function convertToJSON($content){
$json = "[";
$row = explode("\n", $content);
$columnName = explode(",", $row[0]);
$len = count($row)-1;
$columnLen = count($columnName);
for ($i=0; $i < $columnLen; $i++) {
$columnName[$i] = trim($columnName[$i]);
}
for ($i=1; $i < $len; $i++) {
$column = explode(",", $row[$i]);
$json .= "{";
for ($j=0; $j < $columnLen; $j++) {
$json .= ('"' . $columnName[$j] . '":"' . trim($column[$j]) . '"');
if($j<$columnLen-1)
$json .= ',';
}
if($i<$len-1)
$json .= '},';
else
$json .= "}";
}
$json .= "]";
return $json;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment