Skip to content

Instantly share code, notes, and snippets.

@gingermusketeer
Created September 20, 2013 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gingermusketeer/6638728 to your computer and use it in GitHub Desktop.
Save gingermusketeer/6638728 to your computer and use it in GitHub Desktop.
Convert json to php code
function jsonToPhp(json, isNewPhpArrayStyle) {
var phpOpenArray = '[';
var phpCloseArray = ']';
if(isNewPhpArrayStyle !== false) {
isNewPhpArrayStyle = true;
}
if(!isNewPhpArrayStyle) {
phpOpenArray = 'array(';
phpCloseArray = ')';
}
return json.replace(/:/g, '=>').replace(/\{\s*\}/g, "new stdClass()").replace(/{/g, phpOpenArray).replace(/}/g, phpCloseArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment