0

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

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

我的代码是:

 <?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);
?>

但打印出来的是这样的:

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

看起来这与我想要的格式完全不同。还是我在某些方面不正确?

谢谢,亚历克斯

4

2 回答 2

5

似乎您忘记了对 $message 变量进行 json_encode 编码。

<?php echo json_encode($message); ?>
于 2013-03-25T22:38:05.077 回答
1

你需要这样做:echo json_encode($message);

print_r($message);只是转储数组的内容,用于调试。

于 2013-03-25T22:39:18.557 回答