Convert a nested PHP array to a Python nested dictionary

I have a PHP script and I want to write this in Python. So, How to convert this nested PHP array to a 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 this is wrong:

data = {
    'details': {
        {
          {'quick_event': 'Quick'}, 
          {'advance_event': 'Advanced'}
        },
        {
          {'help': 'Help'}
        }
    },
    'has_car': True,
    'has_payment': False
}
+4
source share
2 answers

This question is pretty 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 cards.
+4
source

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

python json .

, , , json python: JSON

+3

Source: https://habr.com/ru/post/1685260/


All Articles