Pass any python dictionary into convertArray.
It will return an array that PHP can read.
Example:
>>> convertArray({"One":1, "Two":[2,"Two"], "Three":[{"ThreeAgain":[3,3,3]} ,[1,2,3], "Three"]})
Returns: array('Three'=>array(array('ThreeAgain'=>array(3, 3, 3)), array(1, 2, 3), 'Three'), 'Two'=>array(2, 'Two'), 'One'=>1)
def convertArray(arr):
ret = ""
list = []
if isinstance(arr, type([]) ):
for ele in arr:
if isinstance(ele, ( type([]), type({})) ):
list.append( convertArray(ele))
elif isinstance(ele, (type(1), type(1.0))):
list.append(str(ele))
else:
list.append("'%s'" % str(ele))
elif isinstance(arr, type({}) ):
for (k,v) in arr.items():
item = "'" + str(k) + "'=>"
if isinstance(v, ( type([]), type({})) ):
item += ( convertArray(v))
else:
if isinstance(v, (type(1), type(1.0))):
item += (str(v))
else:
item += ("'%s'" % str(v))
list.append(item)
else:
raise NameError, "Error - neither a array or a dictionary was passed to this function"
if len(list) > 0:
ret = "array(" + ", ".join(list) + ")"
else:
ret = "array()"
return ret