Created
August 8, 2021 19:01
-
-
Save kernusr/c59d25499eac00b327cef2e48ecdfe32 to your computer and use it in GitHub Desktop.
Print js object, format as PHP assoc array
This file contains hidden or 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
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