What is the equivalent php structure for a python dictionary?

I cannot find how to write empty Python struct / dictionary in PHP. When I wrote "{}" in PHP, this gives me an error. What is the equivalent php programming structure for a Python dictionary?

+5
source share
3 answers

There are associative arrays in php similar to dicionaries. Try looking at the documentation: http://php.net/manual/en/language.types.array.php

In python, you declare an empty dictionary as follows:

m_dictionary = {} #empty dictionary m_dictionary["key"] = "value" #adding a couple key-value print(m_dictionary) 

The way to do the same in php is very similar to the python way:

 $m_assoc_array = array();//associative array $m_assoc_array["key"] = "value";//adding a couple key-value print_r($m_assoc_array); 
+3
source

In PHP Python, dict and list will be the same array() :

 $arr = array(); $arr['a'] = 1; print_r($arr['a']); 
+2
source

If you are trying to pass an empty value from PHP to the Python dictionary, you need to use an empty object, not an empty array.

You can define a new and empty object, for example $x = new stdClass();

0
source

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


All Articles