1

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

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

到目前为止,我有类似的东西

$message = array('aps'=>array('alert'=>$some_variable));

但我很困惑之后如何将“custom_control”的值放入这个数组中。谁能告诉我如何从我现有的php中做到这一点?

谢谢!

4

7 回答 7

5

你是这个意思吗?

<?php
    $some_variable = "Hey";
    $myArray = array(
        "aps" => array(
            "alert" => $some_variable
        ),
        "custom_control" => array(
            "type" => "topic_comment",
            "object" => array(
                "topic_id" => "123",
                "topic_section" => "test",
                "plan_id" => "456"
            )
        )
    );
?>
于 2013-03-25T21:47:22.657 回答
3

这是发现您需要做的事情的简单方法。

  1. 创建您的 JSON 对象。
  2. 将其用作 json_decode 函数的输入。
  3. 使用 this 的输出作为 var_export() 的输入

因此,假设您将 JSON 分配给 $json_object,然后使用:

var_export(json_decode($json_object, true));
于 2013-03-25T21:54:50.277 回答
1

如果您已经创建了初始消息数组(根据您的问题),那么您将执行类似的操作。

$message["custom_control"] = array(
    "type" => "topic_comment",
    "object" => array(
        "topic_id" => "123",
        "topic_section" => "test",
        "plan_id" => "456"
    )
)

您可以通过这种方式在 $message 中创建您需要的任何节点。

于 2013-03-25T21:52:36.280 回答
1

您要创建的不是数组,而是对象。

尽量不要将它构建为一个数组,而是一个对象。

$obj = new stdClass();
$obj->aps = new stdClass();
$obj->aps->alert = 'Hey';
$obj->custom_control = new stdClass();
$obj->custom_control->type = 'topic_comment';
$obj->custom_control->object = new stdClass();
$obj->custom_control->object->topic_id = '123';
$obj->custom_control->object->topic_section = 'test';
$obj->custom_control->object->plan_id = '456';
$json = json_encode($obj); 
于 2013-03-25T21:52:49.933 回答
1

如果您更愿意在 JSON 中构建对象,您可以使用 php.ini 中包含的 JSON 解析器。此外,JSON 定义 Javascript 对象,而不是数组(尽管您可以在 JSON 中定义数组,例如{myArray : [1,2,3]}

如果你想试试这个:http: //php.net/manual/en/function.json-decode.php

于 2013-03-25T21:51:38.440 回答
1
$array = array();
$array['aps'] = "alert";

$array['custom_control'] = array();
$array['custom_control']['type'] = "topic_comment";

$array['custom_control']['object'] = array('topic_id' => '123', 
                                           'topic_section' => 'test', 
                                           'plan_id' => '456');
于 2013-03-25T21:54:08.603 回答
1

我认为你需要这样的东西:

$message =array( "aps" =>array("alert"=>"Hey"),
                  "custom_control" => array(
                                              "type" => "topic_comment",
                                              "object" => array(
                                                                 "topic_id"=>"123",
                                                                 "topic_section"=>"test",
                                                                 "plan_id"=>"456"
                                               )
                                      )
            );
于 2013-03-25T21:54:35.557 回答