Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read a php array from php file in python

Tags:

python

lets say we want to make a python config reader from php.

config.php

$arr = array(
    'a config',
    'b config',
    'c config => 'with values'
)

$arr2 = array(
    'a config',
    'b config',
    'c config => 'with values'
)

readconfig.py

f = open('config.php', 'r')
// somehow get the array then turns it into pythons arr1 and arr2

print arr1
# arr1 = {'a config', 'b config', 'c config': 'with values'}

print arr2
# arr2 = {'a config', 'b config', 'c config': 'with values'}

is this possible in python ?

like image 823
Adam Ramadhan Avatar asked Jan 19 '23 19:01

Adam Ramadhan


1 Answers

from subprocess import check_output
import json

config = check_output(['php', '-r', 'echo json_encode(include "config.php");'])
config = json.loads(config)

where config.php returns an array:

return [
    'param1' => 'value1',
    'param2' => 'value2',
];
like image 66
Eugene Leonovich Avatar answered Jan 30 '23 18:01

Eugene Leonovich