Created
August 6, 2013 00:36
-
-
Save skarred/6160976 to your computer and use it in GitHub Desktop.
PHP function: csv_to_JSON($input, $delimiter, $request)
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 csv_to_JSON($input, $delimiter, $request){ | |
$header = null; | |
$data = array(); | |
$csvData = str_getcsv($input, "\n"); | |
foreach($csvData as $csvLine){ | |
if(is_null($header)) $header = explode($att_delimiter, $csvLine); | |
else{ | |
$items = explode($att_delimiter, $csvLine); | |
for($n = 0, $m = count($header); $n < $m; $n++){ | |
$items[$n] = trim($items[$n]); | |
$prepareData[$header[$n]] = $items[$n]; | |
} | |
$data[] = $prepareData; | |
} | |
} | |
$JSONdata = json_encode($data, JSON_NUMERIC_CHECK); | |
if($request=="[no explanation]JSON[/no explanation]" ) return $JSONdata; | |
elseif($request=="keys") return $header; | |
else return null; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment