Skip to content

Instantly share code, notes, and snippets.

@kernusr
Created August 8, 2021 19:01
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 kernusr/c59d25499eac00b327cef2e48ecdfe32 to your computer and use it in GitHub Desktop.
Save kernusr/c59d25499eac00b327cef2e48ecdfe32 to your computer and use it in GitHub Desktop.
Print js object, format as PHP assoc array
function formatAsPhpArray(obj, depth = 0) {
let buffer = '[\n';
depth++;
Object.entries(obj).forEach(entry => {
const [key, val] = entry;
if (typeof val == 'object') {
buffer += '\t'.repeat(depth) + '"' + key + '" => ' + formatAsPhpArray(val, depth) + ',\n';
} else {
buffer += '\t'.repeat(depth) + '"' + key + '" => "' + val + '",\n';
}
})
buffer += '\t'.repeat(depth-1) + ']'
return buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment