Last active
December 23, 2015 04:29
-
-
Save pepijndevos/6580712 to your computer and use it in GitHub Desktop.
PHP's way of urlencoding nested arrays implemented in Python
This file contains 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
def php_flatten(data): | |
res = {} | |
def inner_flatten(path, value): | |
if isinstance(value, dict): | |
for k, v in value.items(): | |
newpath = "%s[%s]" % (path, k) | |
inner_flatten(newpath, v) | |
elif isinstance(value, list): | |
for k, v in enumerate(value): | |
newpath = "%s[%s]" % (path, k) | |
inner_flatten(newpath, v) | |
else: | |
res[path] = value | |
for k, v in data.items(): | |
inner_flatten(k, v) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment