Question: Convert nested PHP Array to nested Python Dictionary

Question

Convert nested PHP Array to nested Python Dictionary

Answers 2
Added at 2017-09-07 22:09
Tags
Question

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
}
Answers to

Convert nested PHP Array to nested Python Dictionary

nr: #1 dodano: 2017-09-07 22:09

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

nr: #2 dodano: 2017-09-07 23:09

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.
Source Show
◀ Wstecz