Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent php structure to python's dictionary?

Tags:

python

php

struct

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

like image 926
Umidjon Urunov Avatar asked Aug 14 '14 22:08

Umidjon Urunov


2 Answers

In php there are associative arrays, which are similar to dicionaries. Try to have a look to the documentation: http://php.net/manual/en/language.types.array.php

In python, you declare an empty dictionary like that:

m_dictionary = {} #empty dictionary

m_dictionary["key"] = "value" #adding a couple key-value

print(m_dictionary)

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

$m_assoc_array = array();//associative array

$m_assoc_array["key"] = "value";//adding a couple key-value

print_r($m_assoc_array);
like image 156
nbro Avatar answered Oct 04 '22 21:10

nbro


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

$arr = array();
$arr['a'] = 1;
print_r($arr['a']);
like image 44
Ivan Nevostruev Avatar answered Oct 04 '22 21:10

Ivan Nevostruev