2

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"}

3
  • echo json_encode(array("id" => 12)); Commented May 7, 2016 at 14:09
  • 1
    You want to encode your data to JSON, so you don't want to create the JSON yourself. You probably want to do something like this ["id" => $threadID], where you create an array, which you then encode with the function to JSON. Commented May 7, 2016 at 14:10
  • Your $threadtoJ already contains the desired result Commented May 7, 2016 at 14:12

1 Answer 1

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

Is the correct answer. Thanks @u_mulder

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

1 Comment

Indeed, you need a regular array in php, then json_encode it, and you're on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.