Skip to content

Instantly share code, notes, and snippets.

@robert-wallis
Created September 17, 2011 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robert-wallis/1223673 to your computer and use it in GitHub Desktop.
Save robert-wallis/1223673 to your computer and use it in GitHub Desktop.
Convert a standard database array into JSON
<?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