1

I am trying to pass an array from Perl to PHP using JSON-RPC. So here's the Perl routine which forms the arrays and returns it:

sub queueMemberList() : Public()
{       
        my @qNames;
        $connection = ConnectToMySql($dbName);
        $query = "select membername from queue_members";
        $statement = $connection->prepare($query);
        print $statement->execute();
                while(@data = $statement->fetchrow())
                {       #$sendStr = $sendStr."+".$data[0];
                        push(@qNames, $data[0]);
                }
        return \@qNames;
}

sub ConnectToMySql 
{       my ($db) = @_;
        open(ACCESS_INFO, "<accessAdd") || die "Can't access login credentials";
                my $dbName = <ACCESS_INFO>;
                my $host = <ACCESS_INFO>;
                my $userid = <ACCESS_INFO>;
                my $passwd = <ACCESS_INFO>;
                my $connectionInfo="dbi:mysql:$db;$host";
        close(ACCESS_INFO);

        chomp ($dbName, $host, $userid, $passwd);
        my $db_connection = DBI->connect($connectionInfo,$userid,$passwd);
        return $db_connection;
}

As you can see, I've passed an array reference, like I normally would in Perl (and if a Perl program were accepting it, I would convert it back into an array). Now, this is the PHP I have which calls the routine above and attempts to display the array. Since I do not know how to convert it back, I've simple issued and echo statement.

<?php
        ini_set('display_errors',1);
        ini_set('display_startup_errors',1);
        error_reporting(-1);

        require_once '/usr/src/jsonrpcphp/includes/jsonRPCClient.php';
        $myJSONconn2 = new jsonRPCClient('http://localhost:42337/jsonrpc/API/testArray');
        echo "Choose Member:<br>";
        try
        {       echo $myJSONconn2->queueMemberList();

        }
        catch (Exception $e)
        {
                echo nl2br($e->getMessage()).'<br />'."\n";
        }
?>

The Current output simply shows Array on the browser and nothing else.

Could you please help me in converting it back into an array?

EDIT: As suggested by @grebneke, I replaced echo with var_dump which gives me the following output:

array(2) { [0]=> string(5) "messi" [1]=> string(5) "lampard" }

And when I tried it using print_r, this is what I get:

Array ( [0] => messi [1] => rahul )

What does this mean in PHP?

2
  • Use print_r or var_dump to check the data in PHP, that will show you how to proceed. Commented Jan 27, 2014 at 13:46
  • @grebneke - Edited question with the output - What does this mean? Commented Jan 27, 2014 at 15:34

1 Answer 1

2

According to your output of var_dump/print_r, $myJSONconn2->queueMemberList() returns an ordinary PHP array where the first item is "messi" and the second is "rehul":

$response = $myJSONconn2->queueMemberList();  // $response is now an array
print $response[0];                           // this will print: messi

or loop it:

foreach ($response as $data) {
    print $data;
}

I don't know if this is what you're expecting. But no need to "convert it back to an array" - it is an array already.

Sign up to request clarification or add additional context in comments.

3 Comments

so did you mean that $response is an array so I could loop over it using for or while like I normally would for a PHP array?
Yes, it's a completely normal PHP array!
If you will be doing more PHP development, I recommend reading up on var_dump() and print_r(). It is often very useful to dump an object or array to see exactly what data you're working with.

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.