Reputation: 22489
I have a project using python and i want to convert the php to python. I have confused in the array of php in converting it to python...
in the old code of the php... it looks like this,
array(
"Code" => 122,
"Reference" => 1311,
"Type" => 'NT',
"Amount" => 100.00
);
and this is what i did in converting it to python ...
dict = {
"Code":122,
"Reference":1311,
"Type":'NT',
"Amount":100.00
}
is my converting php to python is correct?
Upvotes: 8
Views: 6266
Reputation: 11
PHP arrays are different from py objects that:
array[] = 5
is translated to Python array.append(5)
or
array[None] = 5
,I'd like to give a plug to my newly released product pyx.php
Python module.
There is a Python class array
in the module that emulates a PHP array.
Our Python emulation of the PHP array uses an OrderedDict instance variable,
where array._obj is an OrderedDic to store all elements of the array and
keep track of their orders of insertion using a custom pointer instance
variable. Try:
$ git clone https://github.com/wordpy/pyx/
$ python # or ipython`
>>> import pyx.php as Php; array = Php.array
>>> arr1 = array( (0,'1-0'),('a','1-a'),('b','1-b'),)
>>> arr2 = array( (0,'2-0'),( 1,'2-1'),('b','2-b'),('c','2-c'),)
>>> arr1 + arr2 # same as: Php.array_plus(arr1, arr2), see below
>>> Php.array_merge(arr1, arr2)
>>> import pyx.php as Php; array = Php.array
>>> Arr0 = array() # Arr0._obj is an empty OrderedDict()
>>> Arr1 = array( ('a',11), 'zzz', (99,99), 22, 33, (2,22) )
>>> Arr1
array(6) {
['a']=> <int> 11
[0]=> <str> zzz
[99]=> <int> 99
[100]=> <int> 22
[101]=> <int> 33
[2]=> <int> 22
}
zip()
works for array with different len !!!
>>> for i,j in zip( array(1,2,3,4), array(11,22,33) ):
... print(i,j)
1 11
2 22
3 33
>>> for i,j in zip( array(1,2), array(11,22,33) ):
... print(i,j)
1 11
2 22
array()
in the pyx.php
Cython module offers almost everything that
a PHP array() offers, plus many more methods.
Please see https://wordpy.com/pyx/php/.
Currently, pyx.php
is only available for Python 3.x running 64-bit Linux.
Python 2.x, Mac, or other platforms can be compiled when there are many
requests.
Upvotes: 0
Reputation: 1678
To be specific, a PHP associative array is the same as a Python dictionary, and the PHP associative array is ordered. Ruby hash tables are same as PHP associative arrays (ordered key-value pairs).
Python lists and PHP arrays are the same (unordered lists of non-keyed values). Ruby arrays are the same as Python and PHP.
Upvotes: 0
Reputation: 226346
Your conversion is essentially correct (though I wouldn't use dict as a variable name since that masks a built-in class constructor of the same name). That being said, 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
>>> od = collections.OrderedDict([
('Code', 122),
('Reference', 1311),
('Type', 'NT'),
('Amount', 100.00),
])
>>> print od['Amount']
100.0
>>> od.keys()
['Code', 'Reference', 'Type', 'Amount']
Upvotes: 12