Skip to content

Instantly share code, notes, and snippets.

@willwade
Created March 11, 2012 08:32
Show Gist options
  • Save willwade/2015611 to your computer and use it in GitHub Desktop.
Save willwade/2015611 to your computer and use it in GitHub Desktop.
JSON to CSV
<?php
error_reporting(E_ALL);
$json_string = file_get_contents('pass.json');
$json_decode = json_decode2($json_string);
var_dump($json_decode);
exit;
$csvstring = "'" . implode("','", $json_decode) . "'";
echo $csvstring;
#if ( !function_exists('json_decode') ){
function json_decode2($json)
{
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($json); $i++)
{
if (!$comment)
{
if (($json[$i] == '{') || ($json[$i] == '[')) $out .= ' array(';
else if (($json[$i] == '}') || ($json[$i] == ']')) $out .= ')';
else if ($json[$i] == ':') $out .= '=>';
else $out .= $json[$i];
}
else $out .= $json[$i];
if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment;
}
eval($out . ';');
return $x;
}
#}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment