Genadinik
Genadinik

Reputation: 18639

PHP - trying to make a JSON-like array and getting confused

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!

Upvotes: 1

Views: 90

Answers (7)

Captain Payalytic
Captain Payalytic

Reputation: 1071

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

Upvotes: 3

Fabrizio Duroni
Fabrizio Duroni

Reputation: 777

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

Upvotes: 1

user2189593
user2189593

Reputation:

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

Upvotes: 1

Mike Brant
Mike Brant

Reputation: 71384

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

Upvotes: 1

miah
miah

Reputation: 10433

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.

Upvotes: 1

JakeCataford
JakeCataford

Reputation: 141

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

Upvotes: 1

h2ooooooo
h2ooooooo

Reputation: 39532

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

Upvotes: 5

Related Questions