4

I have a PHP script and want to write that in Python. So, How can I convert this nested PHP Array to nested python dictionary?

$data = [
    'details'=> [
        [
          ['quick_event'=> 'Quick'], 
          ['advance_event'=> 'Advanced']
        ],
        [
          ['help'=> 'Help']
        ]
    ],
    'has_car'=> true,
    'has_payment'=> false
];

I created this in Python but it's wrong:

data = {
    'details': {
        {
          {'quick_event': 'Quick'}, 
          {'advance_event': 'Advanced'}
        },
        {
          {'help': 'Help'}
        }
    },
    'has_car': True,
    'has_payment': False
}
0

2 Answers 2

4

This question is rather narrow but here we go:

data = {
    'details': [
        [
          {'quick_event': 'Quick'}, 
          {'advance_event': 'Advanced'}
        ],
        [
          {'help': 'Help'}
        ]
    ],
    'has_car': True,
    'has_payment': False
};
>>> data
{'details': [[{'quick_event': 'Quick'}, {'advance_event': 'Advanced'}], [{'help': 'Help'}]], 'has_car': True, 'has_payment': False}

In a nutshell:

  • Convert => to :
  • Convert [] to {} for maps.
Sign up to request clarification or add additional context in comments.

2 Comments

this definitely fixes the immediate problem
The answer is great. My problem was fixed.
3

In php use http://php.net/manual/en/function.json-encode.php to format the data as json.

In python convert the json to a dictionary.

Maybe check this link to see how to convert json to dictionary in python: Converting JSON String to Dictionary Not List

3 Comments

I have a PHP script and want to write that in Python. I can't do this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.