9

I'm very new to php and I've been spending quite some type understanding how to pass arguments from Python to php and conversely. I now know how to pass single variables, but I am still stuck and can't seem to find an answer to this one:

Php calls a Python script (that part works) that returns a list of strings. I'd like to process this list in php.

When I try:

print mylist

in myscript.py, and then :

$result = exec('python myscript.py')

it looks like php understands $result as a single string (which I agree makes sense).

I understand that maybe json can help or that I somehow need to use a dictionary instead of a list in python. However I can't figure out how exactly.

If anyone can help, it will be much appreciated! Thanks!

cenna75
  • 539
  • 1
  • 5
  • 13

4 Answers4

14

For instance:

myscript.py

import json

D = {'foo':1, 'baz': 2}

print json.dumps(D)

myscript.php

<?php 

$result = json_decode(exec('python myscript.py'), true);
echo $result['foo'];
Krab
  • 6,526
  • 6
  • 41
  • 78
  • Thanks for the quick answer! I can see what I did wrong: I thought I was passing a list when I was actually trying to pass a numpy array. For the record, that does not work with json! Thanks again! – cenna75 Jun 21 '13 at 12:57
2

You're using stdin / stdout to transfer the data between the programs, that means you'll have to encode your structure somehow in order to let your receiving program parse the elements.

The simplest thing would be to have python output something like a comma separated list

Adam,Barry,Cain

and use

$result = explode(exec('python myscript.py'));

on the php side to turn your string data back into an array.

If the data is unpredictable (might contain commas) or more structured (more than just a simple list) then you should go for something like json as suggested by Krab.

Loopo
  • 2,204
  • 2
  • 28
  • 45
  • 1
    Alright, that makes sense as well. Are you suggesting that json should be avoided when possible? Does it have to do with security or processing time maybe? I only have terrible notions in these areas... – cenna75 Jun 21 '13 at 14:15
  • 1
    not at all... if you're doing anything beyond a simple list, and maybe even if you are doing a simple list, you should use something like json or xml. And usually there are libraries available to make encoding/decoding fairly simple. I was more putting in the simple list to demonstrate the principle of passing 'arrays' between programs via a stream (file/stdin/http) – Loopo Jun 21 '13 at 16:49
  • Got it! Thanks for educating a newbie :) – cenna75 Jun 24 '13 at 14:00
1

Apparently your question was misleading. Was redirected here thinking this a solution for converting python lists to php array.

Posting a naive solution for ones wanting to convert lists to php array.

// Sample python list 
$data = '[["1","2","3","4"],["11","12","13","14"],["21","22","23","24"]]';

// Removing the outer list brackets
$data =  substr($data,1,-1);

$myArr = array();
// Will get a 3 dimensional array, one dimension for each list
$myArr =explode('],', $data);

// Removing last list bracket for the last dimension
if(count($myArr)>1)
$myArr[count($myArr)-1] = substr($myArr[count($myArr)-1],0,-1);

// Removing first last bracket for each dimenion and breaking it down further
foreach ($myArr as $key => $value) {
 $value = substr($value,1);
 $myArr[$key] = array();
 $myArr[$key] = explode(',',$value);
}

//Output
Array
(
[0] => Array
    (
        [0] => "1"
        [1] => "2"
        [2] => "3"
        [3] => "4"
    )

[1] => Array
    (
        [0] => "11"
        [1] => "12"
        [2] => "13"
        [3] => "14"
    )

[2] => Array
    (
        [0] => "21"
        [1] => "22"
        [2] => "23"
        [3] => "24"
    )

)
Kushal
  • 423
  • 5
  • 8
1
$str = "[u'element1', u'element2', 'element3']";

$str = str_replace( array("u'", "[", "]"), array("'", ""), $str );
$strToPHPArray = str_getcsv($str, ",", "'");
print_r( $strToPHPArray );

Outputs

Array
(
    [0] => element1
    [1] => element2
    [2] => element3
)
Jake Sully
  • 31
  • 4