1

I've been doing some research around and haven't found a way to solve the situation I'm faced with now.

I need to pass a Python list to PHP. I've been reading about doing it with JSON but I was wondering if it was possible without it.

My list looks something like this:

a_list = [0,['A1', 'A2', ['A3','A4']], ['B5', 'B2', ['B3','B4']]]

I have found how to pass simple values between Python and PHP but this is a bit more complicated.

Also, I have found on another question asked here something that works for dictionaries:

Python side:

import json

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

print json.dumps(D)

PHP side:

<?php 

$result = json_decode(exec('python myscript.py'), true);
echo $result['foo'];

Any help would be appreciated.

Thanks!

2
  • There is also external phpserializer library. Commented Jul 16, 2013 at 15:55
  • 1
    The data needs to be serialized in one form or another. JSON is a simple and clean format (and human-readable) and is widely supported. You could use a dozen other encodings, but they'll only add to the complexity. Also, since you're exec-ing, you won't notice any performance difference from different encodings. Commented Jul 16, 2013 at 15:55

1 Answer 1

1

You'll need to serialize the data to somehow, to send it across. Here are a couple of ways to do it:

  1. JSON
  2. Protobuf
  3. Write the data out to a text file in python and read it back in with PHP
  4. Start a server in your PHP program and have your python program write the data to it in some format over a socket
Sign up to request clarification or add additional context in comments.

2 Comments

hmm... re: #3 could that be a session file I wonder .... if it was PHP that passed Python the key?
possibly, but you'd have to ensure that PHP flushes everything to that file and closes it first; then, you'd open, write to it, and close it in python; an reopen in PHP. Basically, you want to avoid corruption at writing time. NB: Session file is not $_SESSION. It would be very awesome though, if I could get the memory address of a new empty PHP array and get python to write bytes directly to that memory address

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.