سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top