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. これの出力をvar_export()への入力として使用します

したがって、JSONを$ json_objectに割り当てたとすると、次を使用します。

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

JSONでオブジェクトを作成する方が快適な場合は、phpに含まれているJSONパーサーを使用できます。また、JSONは配列ではなくJavascriptオブジェクトを定義します(ただし、JSONで配列を定義するには次のようなものを使用できます){myArray : [1,2,3]}

ただし、必要に応じてこれを試してください:http: //php.net/manual/en/function.json-decode.php

于 2013-03-25T21:51:38.440 に答える
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
$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 に答える