![](/img/trans.png)
[英]How to pass variable from python script into php and show it in webpage
[英]How to pass an array to php from python script?
我正在從我的php運行python腳本:
<?php
$command = escapeshellcmd('<path>/myscript.py');
$output = shell_exec($command);
echo $output;
?>
現在,我需要將字符串的二維數組從python腳本傳遞到php頁面,但是我很困惑。 當然, $output
將包含整個數組的單個字符串。 我也考慮過json,但是在那種情況下,我應該將python數組轉換成json字符串(這可能是另一個問題)。
如果您的數組不太特殊,那么您的JSON想法實際上應該可以正常工作。 我構建了一個小python腳本( test.py
)來提供php腳本( 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);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.