user1789437
user1789437

Reputation: 468

JSON Encode with [ ]

I cant seem to get my json_encode to include brackets.. Any help?

My code is:

$th = json_encode(array("4" => "four", "8" => "eight"));
echo $th;

and the output is:

{"4":"four","8":"eight","9":"\n"}

The output I need to send to the api is:

[{"4":"four","8":"eight","9":"\n"}]

How can I get the brackets in the JSON array? Thank you!

Upvotes: 0

Views: 153

Answers (2)

admoghal
admoghal

Reputation: 1548

Use this

$th = json_encode(array(array("4" => "four", "8" => "eight", "9" => "\n")));

Upvotes: 0

jaco
jaco

Reputation: 3506

You should do this way:

$th = json_encode(array(array("4" => "four", "8" => "eight")));

Upvotes: 3

Related Questions