Skip to content

Instantly share code, notes, and snippets.

@pepijndevos
Last active December 23, 2015 04:29
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 pepijndevos/6580712 to your computer and use it in GitHub Desktop.
Save pepijndevos/6580712 to your computer and use it in GitHub Desktop.
PHP's way of urlencoding nested arrays implemented in Python
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