user
user

Reputation: 545

Json encode vs string array

I'm using the json_encode function to transform a php array to a json. Now, i want to generate something like this :

"Strings": ["String 1","String 2","String 3"]

I don't know how to do ?

I tried :

$array["strings"] = '["String 1","String 2","String 3"]';

But it doesn't work as i get this as result :

"Strings":"[\"String 1\", \"String 2\", \"String 3\"]"

Then i tried this

$array["strings"] = '[String 1,String 2,String 3]';

It doesn't work, i got this as result :

"Strings":"[String 1, String 2, String 3]"

Can someone help me please ?

Thx.

Upvotes: 0

Views: 61

Answers (2)

Jesper We
Jesper We

Reputation: 6087

json_encode( array( "strings"=>array("string1", "string2", "string3" ) ) )

Upvotes: 1

Steven Cranham
Steven Cranham

Reputation: 31

You have to define it like this

$array["String"] = ["String 1","String 2","String 3"];

then you output will be:

"String":["String 1","String 2","String 3"]

Upvotes: 1

Related Questions