Skip to content

Instantly share code, notes, and snippets.

@dhensby
Last active December 10, 2015 23:39
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 dhensby/4511382 to your computer and use it in GitHub Desktop.
Save dhensby/4511382 to your computer and use it in GitHub Desktop.
Convert SS_Query results to JSON
<?php
function queryToJSON(SS_Query $qry) {
//the keys you want to code into JSON, this is to white list your array
$jsonKeys = array(
'ID' => 'ID',
'Title' => 'Title',
'Content' => 'Content'
);
$dataToEncode = array();
while ($record = $qry->next()) {
$dataToEncode[$record['ID']] = array_intersect_key($record,$jsonKeys);
}
return Convert::array2json($dataToEncode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment