如何从python脚本将数组传递给php? [英] How to pass an array to php from python script?
问题描述
我正在从我的php运行python脚本:
I am running a python script from my php:
<?php
$command = escapeshellcmd('<path>/myscript.py');
$output = shell_exec($command);
echo $output;
?>
现在,我需要将一个二维字符串数组从python脚本传递到php页面,但是我很困惑.当然,$output
将包含整个数组的单个字符串.我也考虑过json,但在那种情况下,我应该将python数组转换成json字符串(这可能是另一个问题)....
Now I am in the need to pass a bidimensional array of strings from the python script to the php page, but I am quite stuck. Of course, $output
will contain a single string of the whole array. I also thought about json, but in that case I should convert the python array into a json string (this could be another question)....
推荐答案
如果您的数组不是太奇特,那么您的JSON想法实际上应该可以正常工作.我构建了一个小python脚本(test.py
)来喂养php脚本(test.php
):
If your array isn't too exotic, your JSON idea should actually work just fine. I built a little python script (test.py
) to feed the php script (test.php
):
test.py:
import json
print json.dumps([['a','b','c'], ['d','e','f']]);
test.php:
<?php
$command = escapeshellcmd('python test.py');
$output = shell_exec($command);
echo $output;
$arr = json_decode($output);
var_dump($arr);
?>
这篇关于如何从python脚本将数组传递给php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!