1

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"
        }
    }
}

So far I have something like

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

but I am getting confused how I can put the values for "custom_control" into this array after that. Could anyone please advise how to do that from my existing php?

Thanks!

7 Answers 7

5

Is this what you mean?

<?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"
            )
        )
    );
?>
Sign up to request clarification or add additional context in comments.

3 Comments

if you want it in actual json format for a service, use exit(json_encode($myArray)); after the array declaration here ^
Got it. Thank you guys! I will accept the answer as soon as SO allows me to do that.
This will not give you the JSON you want unless you pass JSON_FORCE_OBJECT option in json_encode() since the JSON you have posted in you question is an object representation not an array representation.
3

Here is an easy way to discover what you need to do.

  1. Create your JSON object.
  2. Use it as input to the json_decode function.
  3. Use the output of this as the input to var_export()

So supposing you assigned your JSON to $json_object, then use:

var_export(json_decode($json_object, true));

2 Comments

Though not a specific answer to the question, I certainly like the approach here of giving the OP a useful tool for figuring this out themselves.
More useful than a specific answer. Give a man a fish (the specific answer) and you'll feed him for a day. Teach him to fish (my answer) and he can easily work out the constructions whenever he needs to :-)
1

If you are more comfortable building the object in JSON you can use the JSON parser included in php. Also, JSON defines Javascript objects, not arrays (although you can define arrays in JSON with something like {myArray : [1,2,3]}

Try this if you want though: http://php.net/manual/en/function.json-decode.php

Comments

1

If you've already created your initial message array (per your question), you would then do something like this.

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

You can create whatever nodes you needs inside of $message this way.

Comments

1

What you are trying to create is not an array, but rather an object.

Try to not build it as an array but an object.

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

1 Comment

If anyone looks at this in the future, simply changing all array('s to (object)array( might be simpler, dependent on your current design. Another lazy method is to simply json_decode(json_encode($recursiveArray)).
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');

Comments

1

i think you need something like this:

$message =array( "aps" =>array("alert"=>"Hey"),
                  "custom_control" => array(
                                              "type" => "topic_comment",
                                              "object" => array(
                                                                 "topic_id"=>"123",
                                                                 "topic_section"=>"test",
                                                                 "plan_id"=>"456"
                                               )
                                      )
            );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.