Umidjon Urunov
Umidjon Urunov

Reputation: 671

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

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?

Upvotes: 13

Views: 7826

Answers (3)

fullerja
fullerja

Reputation: 143

If you are trying to pass a null value from PHP to a Python dictionary, you need to use an empty object rather than an empty array.

You can define a new and empty object like $x = new stdClass();

Upvotes: 0

nbro
nbro

Reputation: 15847

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);

Upvotes: 13

Ivan Nevostruev
Ivan Nevostruev

Reputation: 28723

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

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

Upvotes: 2

Related Questions