3

I am running a python script from my php:

<?php 

$command = escapeshellcmd('<path>/myscript.py');
$output = shell_exec($command);
echo $output;
?>

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)....

1
  • 2
    You'll need some kind of interchange format that python can produce and PHP can understand. JSON seems like a nice standardized option, in my opinion. Commented Sep 14, 2017 at 19:17

1 Answer 1

7

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);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

json_decode has a parameter to give you an associative array, like this: json_decode($output, true) php.net/json_decode
Look at my (updated) answer: It has a bi-dimensional array in python and also in PHP.
btw, for me this doesn't work as expected with the single quotes around the letters, may need double

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.