135

I am trying to create an JSON object out of a PHP array. The array looks like this:

$post_data = array('item_type_id' => $item_type, 'string_key' => $string_key, 'string_value' => $string_value, 'string_extra' => $string_extra, 'is_public' => $public, 'is_public_for_contacts' => $public_contacts); 

The code to encode the JSON look like this:

$post_data = json_encode($post_data); 

The JSON file is supposed to look like this in the end:

{ "item": { "is_public_for_contacts": false, "string_extra": "100000583627394", "string_value": "value", "string_key": "key", "is_public": true, "item_type_id": 4, "numeric_extra": 0 } } 

How can I encapsulate the created JSON code in the "item": { JSON CODE HERE }.

5 Answers 5

195

Usually, you would do something like this:

$post_data = json_encode(array('item' => $post_data)); 

But, as it seems you want the output to be with "{}", you better make sure to force json_encode() to encode as object, by passing the JSON_FORCE_OBJECT constant.

$post_data = json_encode(array('item' => $post_data), JSON_FORCE_OBJECT); 

"{}" brackets specify an object and "[]" are used for arrays according to JSON specification.

Sign up to request clarification or add additional context in comments.

5 Comments

i would add the JSON_FORCE_OBJECT in json_encode($arr, JSON_FORCE_OBJECT)
Is this correct? $post_data = json_encode(array('item' => $post_data), JSON_FORCE_OBJECT);
what if I have an array somewhere nested inside $post_data. This would make them objects as well, correct?
echo json_encode(array('item' =>$post_data)); will create the JSON structure of: Object, Array, Object. or: { [ { Which is exactly what I was looking for, importing MySQL JSON response into an iOS app :-) THANKS Cristian!!!
You don't need JSON_FORCE_OBJECT when the keys are strings. It will always produce an object for this.
91

Although the other answers posted here work, I find the following approach more natural:

$obj = (object) [ 'aString' => 'some string', 'anArray' => [ 1, 2, 3 ] ]; echo json_encode($obj); 

3 Comments

This response is so good. Also when you don't control exactly when an object is going to be encoded or if you want to encode an array of objects: then the JSON_FORCE_OBJECT response does not work. On the other side is much more readable. Thanks!
If you are looking for an encode that begins as an object and proceeds to contain arrays, this is your answer.
Actually for my case it converts into JSON string Object like this string(36) "{"aString":626,"anArray":"OFZgavNT"}" not like original JSON Object like below object(stdClass)#27 (2) { ["id"]=> int(626) ["firm_id"]=> int(63)}
40

You just need another layer in your php array:

$post_data = array( 'item' => array( 'item_type_id' => $item_type, 'string_key' => $string_key, 'string_value' => $string_value, 'string_extra' => $string_extra, 'is_public' => $public, 'is_public_for_contacts' => $public_contacts ) ); echo json_encode($post_data); 

Comments

6

You could json encode a generic object.

$post_data = new stdClass(); $post_data->item = new stdClass(); $post_data->item->item_type_id = $item_type; $post_data->item->string_key = $string_key; $post_data->item->string_value = $string_value; $post_data->item->string_extra = $string_extra; $post_data->item->is_public = $public; $post_data->item->is_public_for_contacts = $public_contacts; echo json_encode($post_data); 

Comments

0
$post_data = [ "item" => [ 'item_type_id' => $item_type, 'string_key' => $string_key, 'string_value' => $string_value, 'string_extra' => $string_extra, 'is_public' => $public, 'is_public_for_contacts' => $public_contacts ] ]; $post_data = json_encode(post_data); $post_data = json_decode(post_data); return $post_data; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.