Skip to content
Advertisement

php json response with curly bracket [closed]

The problem is to get an object result instead of array in json.

here is my code :

$response = $obj->toArray();
$encoded = json_encode($response, true);
header('Content-type: application/json');
exit($encoded); 

and the result is somtimes like :

[ {"id":"1","value":"COM1"} , {"id":"2","value":"COM2"} ]

and I want it a pure json like :

{ "0" : {"id":"1","value":"COM1"} , "1" : {"id":"2","value":"COM2"} }

Advertisement

Answer

Try something like this:

json_encode( $array, JSON_FORCE_OBJECT )

That will return everything encased in { }

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement