Skip to main content

Convert php array to python dictionary

Php array is combination of key and value pair in ordered manner. Which is allow to add element key with and without keys. OrderedDict in python are capable to deal with similar functionality in Php array.

Try Live example.


Example to convert array

The php array is like a dictionary which internally assigns the key to every array element.

Example A

Php code

<?php
  // Simple array with only values
  $simple = array(10,20,30,40,50);
  print_r($simple);
?>

Php Output


Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Equivalent python code

from collections import OrderedDict
# Simple OrderedDict
simple = OrderedDict([(0,10),(1,20),(2,30),(3,40),(4,50)]);
print(simple);

Python Output

OrderedDict([(0, 10), (1, 20), (2, 30), (3, 40), (4, 50)])

Example B

Php code

<?php
// Combination of normal value and key pair
$fruits = array("mango","orange",12 => "banana","pineapple","grapes");
// Display fruits
print_r($fruits);
?>

Php Output

Array
(
    [0] => mango
    [1] => orange
    [12] => banana
    [13] => pineapple
    [14] => grapes
)

Equivalent python code

from collections import OrderedDict
# Combination of normal value and key pair
fruits = OrderedDict([(0,"mango"),(1,"orange"),(12,"banana"),(13,"pineapple"),(14,"grapes")]);
# Display fruits
print(fruits);

Python Output

OrderedDict([(0, 'mango'), (1, 'orange'), (12, 'banana'), (13, 'pineapple'), (14, 'grapes')])

Note above python code is produced by our tool. See some more advanced scenarios for dealing with PHP array methods.

Example C

Php code

<?php
  // Array
  $arr = array();
  print_r($arr);
  $arr[] = 10;
  $arr[4] = 20;
  $arr[-2] = 20;
  $arr["two"] = 2;
  $arr[] = 20;
  print_r($arr);
?>

Php Output

Array
(
)
Array
(
    [0] => 10
    [4] => 20
    [-2] => 20
    [two] => 2
    [5] => 20
)

Equivalent python code

from collections import OrderedDict

# Manage core logic by this class
class Settlement :
    @staticmethod
    def default_key(d):
        result = 0
        for key, _ in d.items():
            # Check key is integer and key is not less than result
            if(type(key) is int and key >= result) :
                # Get new key
                result = key + 1
        return result
    
# Array
arr = OrderedDict([]);
print(arr);
arr[Settlement.default_key(arr)] = 10;
arr[4] = 20;
arr[-2] = 20;
arr["two"] = 2;
arr[Settlement.default_key(arr)] = 20;
print(arr);

Python Output

OrderedDict()
OrderedDict([(0, 10), (4, 20), (-2, 20), ('two', 2), (5, 20)])

Example D

Php code

<?php
$num = array(1,3,6,2,9);
$fruits = array("mango","orange",12 => "banana","pineapple","grapes");

// Test A
foreach($num as $value) {
    echo  $value,"\n";
}
// Test B
foreach ($fruits as $key => $value) {
    echo $key,"=>",$value,"\n";
}
?>

Php Output

1
3
6
2
9
0=>mango
1=>orange
12=>banana
13=>pineapple
14=>grapes

Equivalent python code

from collections import OrderedDict
num = OrderedDict([(0,1),(1,3),(2,6),(3,2),(4,9)]);
fruits = OrderedDict([(0,"mango"),(1,"orange"),(12,"banana"),(13,"pineapple"),(14,"grapes")]);
# Test A
for value in num.values() : 
    print(value, "\n",end="");

# Test B
for key,value in fruits.items() :
    print(key, "=>", value, "\n",end="");

Python Output

1
3
6
2
9
0 => mango
1 => orange
12 => banana
13 => pineapple
14 => grapes




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment