Michael Gulik
Michael Gulik

Reputation: 86

Create a JSON object from a single PHP variable with key

I am trying to send a single PHP variable to a client via json. However, if I generate the string myself and pass it into json_encode, it adds / around the key. I am using:

$threadID = mysqli_insert_id($Thesisdb);
$threadtoJ='{"id":'.$threadID.'}';
echo json_encode($threadtoJ);

I am looking for an output like this: {"id","12"}

Upvotes: 2

Views: 361

Answers (1)

Michael Gulik
Michael Gulik

Reputation: 86

echo json_encode(array("id" => 12));

Is the correct answer. Thanks @u_mulder

Upvotes: 3

Related Questions