Created
September 17, 2011 06:00
-
-
Save robert-wallis/1223673 to your computer and use it in GitHub Desktop.
Convert a standard database array into JSON
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 | |
// Copyright (C) 2007 Robert Wallis - July 11, 2007 | |
function json_database($recordset) | |
{ | |
if (0==count($recordset)) | |
return '[]'; | |
$o = "[\n"; | |
foreach($recordset as $records) | |
{ | |
$o .= '{'; | |
foreach ($records as $column=>$value) | |
{ | |
if (is_null($value)) | |
$o .= addslashes($column).':null,'; | |
else if (is_numeric($value)) | |
$o .= addslashes($column).':'.$value.','; | |
else | |
$o .= addslashes($column).':"'.addslashes($value).'",'; | |
} | |
$o = substr($o, 0, -1); | |
$o .= "},\n"; | |
} | |
$o = substr($o, 0, -2); | |
$o .= "\n]"; | |
return $o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment