PHP - 打印出一个数组似乎没有产生我的意图

Gen*_*nik 0 php json

我正在使用PHP并尝试创建一个看起来像这样的数组:

{
    "aps" : {
        "alert" : "Hey"
    },
    "custom_control" : {
        "type" : "topic_comment",
        "object":{
            "topic_id":"123",
            "topic_section":"test"
                        "plan_id":"456"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的代码是:

 <?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);
?>
Run Code Online (Sandbox Code Playgroud)

但打印出的是这样的:

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

看起来这是一种完全不同于我想要的格式.或者我在某种程度上是不正确的?

谢谢,亚历克斯

Rap*_*eta 5

好像你忘记了json_encode你的$ message变量.

<?php echo json_encode($message); ?>
Run Code Online (Sandbox Code Playgroud)