1

There are similar questions I have not understood explanation (my question is simpler..) inside php :

echo json_encode(array("value" => $data));

returns me a JSON line like:

{"value":"100"} (assuming 100 is $data)

How can I get a json like :

{"value":100} ($data NOT doublequoted) ??

Thank you

4
  • 1
    You're probably after the JSON_NUMERIC_CHECK encode option. Commented Nov 16, 2016 at 15:05
  • Technically, all JSON in PHP should be wrapped in "" (double quotes), even the PHP-variables. PHP doesn't differentiate between "100" and 100, so I don't really understand the need. Commented Nov 16, 2016 at 15:08
  • JSON_NUMERIC_CHECK works IF the element of the array is a number, if like my case it is a variable, array("value" => $data) it doesn't work , still return the numeric value of $data doublequoted, any workaround ? Commented Nov 17, 2016 at 7:43
  • YOU ARE CORRECT ! JSON_NUMERIC_CHECK works also if the element is a variable !! My mistake the $data variable content was not pure numeric THANKS Commented Nov 17, 2016 at 8:26

1 Answer 1

4

You should probably use JSON_NUMERIC_CHECK as the 2nd argument to json_encode() method of PHP like this:

$arr = ['value' => '100'];
$json_encoded = json_encode($arr, JSON_NUMERIC_CHECK);
// echo $json_encoded --- "{"value":100}"

Hope this helps!

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

1 Comment

this is not worknig for comma seperated values

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.