tags:

views:

255

answers:

5

how do i pass php array's to ruby script as an argument so that ruby script can read php's array. ?

+2  A: 

You could serialize it to YAML or JSON.

Chuck
Or any neutral serialized format for that matter (XML, CSV, etc.).
Matthew Vines
okay, i serialized the array to JSON....and i pass it as argument to ruby script....but ruby complains because of the double quotes.... additionally, how do i parse JSON and deserialize it in ruby ?
+2  A: 

You could use JSON:

$ cat f1.php
<?php
$a = array(1, 2, 3);
$cmd = 'ruby f2.rb \'' . json_encode($a) . '\'';
printf("%s\n", `$cmd`);
?>
$ cat f2.rb
require 'rubygems'
require 'json'

s = JSON.parse ARGV[0]
puts s
puts s.class
$ php f1.php
1
2
3
Array
DigitalRoss
yes.... i am doing this method...however, php f1.php doesn't "puts" 1 2 3....
Hmm, you mean you can't reproduce my example? Perhaps we should simplify things slightly and try:$ ruby f2.rb '[1,2,3]'
DigitalRoss
that works.... but when i do exec("ruby f2.rb '[1,2,3]'")the php script runs, and there is no ruby "puts"
strange...it works with printf("%s\n", `$cmd`);
The backtick operator is equivalent to shell_exec() rather than exec(), but note the optional second parameter to exec(). The actual return value from exec() is just the last line from the command output.
DigitalRoss
A: 

something like this this

require 'json'

data = ARGV[0]

result = JSON.parse(data)
ez
A: 

You can try serializing it yourself

1,67,12,320,341,901,77


0, 1, 2, 3, 4, 5, 6

You can use phps explode function to do this. Put the array in a hidden tag and let ruby dissaymble the array.

A: 

JSON is a better idea, but if you must use PHP's serialize() function you can unserialize it in Ruby using this library: http://www.aagh.net/projects/ruby-php-serialize

dubek