Python Dict versus PHP array

PHP arrays are ordered mappings, so you should use a Python OrderedDict instead of a regular dict so that the order of insertion gets preserved.

import collections
my_dict = collections.OrderedDict([
('name1', 0),
('name2', 1),
('name3', '2'),
('name4', '11:11'),])

This is the same in PHP of :

my_array = array(
"name1" => 0,
"name2" => 1,
"name3" => '2',
"name4" => '11:11'
);

If you use just a regular DICT in python, when you make a “for item in my_dict:” the order of iterations is not the order of insertions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s