Last active
August 29, 2015 14:06
-
-
Save yunchih/52797a5bdd1d70362ef7 to your computer and use it in GitHub Desktop.
This file contains 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 | |
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