Genadinik
Genadinik

Reputation: 18639

PHP - printing out an array does not seem to produce what I intended

I am using PHP and trying to create an array that looks something like this:

{
    "aps" : {
        "alert" : "Hey"
    },
    "custom_control" : {
        "type" : "topic_comment",
        "object":{
            "topic_id":"123",
            "topic_section":"test"
                        "plan_id":"456"
        }
    }
}

The code I have is:

 <?php
    $message = array(
        "aps" => array(
            "alert" => "hey"
        ),
        "custom_control" => array(
            "type" => "topic_comment",
            "object" => array(
                "topic_id" => "123",
                "topic_section" => "abc",
                "plan_id" => "456"
            )
        )
    );

print_r($message);
?>

but what is printed out is this:

Array ( [aps] => Array ( [alert] => hey ) [custom_control] => Array ( [type] => topic_comment [object] => Array ( [topic_id] => 123 [topic_section] => abc [plan_id] => 456 ) ) )

It seems like this is a totally different format from what I had intendd. Or am I incorrect in some way?

Thanks, Alex

Upvotes: 0

Views: 42

Answers (2)

greg
greg

Reputation: 6913

You need to do this: echo json_encode($message);

print_r($message); just dumps the contents of the array, used it for debugging.

Upvotes: 1

Raphael Caixeta
Raphael Caixeta

Reputation: 7846

It seems like you forgot to json_encode your $message variable.

<?php echo json_encode($message); ?>

Upvotes: 5

Related Questions