views:

66

answers:

1

I'm sending MapMessages in Java to ActiveMQ and retrieving them using Stomp in PHP. My message creation code looks like this:

MapMessage message = session.createMapMessage();
message.setInt("id", 42);
message.setInt("status", 42);
message.setString("result", "aString");

When I retrieve them in PHP, the array that's created looks like this:

Array (
[map] => Array (
        [0] => Array (
                [entry] => Array (
                        [0] => Array (
                                [string] => id
                                [int] => 42
                            )

                        [1] => Array (
                                [string] => status
                                [int] => 42
                            )

                        [2] => Array (
                                [string] => Array (
                                        [0] => result
                                        [1] => aString
                                    )
                            )
                    )
            )
    )
)

What am I supposed to do with that? Is there a way to convince Stomp to unserialize it in a reasonable manner or is there some PHP incantation make accessing this array less painful? In particular, I can't just iterate through the entries and build an associative array because the array looks completely different if there is a string & int as opposed to two strings.

A: 

Here's what I've come up with. Does anyone know of a cleaner solution?

$entries = $msg->map['map'][0]['entry'];
$map = array();
foreach($entries as $entry) {
    $vals = array_values($entry);
    if(count($vals) == 1 && is_array($vals[0])) {
        $vals = $vals[0];
    }
    $map[$vals[0]] = $vals[1];
}

This gives me:

array
  'id' => int 42
  'status' => int 42
  'result' => string 'aString' (length=7)

which is pretty much what I'm looking for, but the code to get there seems pretty fragile.

scompt.com