How to remove quotes from numerical array value How to remove quotes from numerical array value json json

How to remove quotes from numerical array value


You're quoting the zero, making it a string. Just leave it as a int:

<?php$data = array('eCode' => 0, 'city' => 'london', 'time' => '15:35');echo json_encode($data);?>


This answer reiterates some things that were already said by others, but hopefully a little more explanation will be helpful.

Basically, the zero is quoted in the output of json_encode($data) because it is quoted in the input.

json_encode will not automatically convert numeric strings to integers, unless you have set the JSON_NUMERIC_CHECK option, for example:

echo json_encode($data, JSON_NUMERIC_CHECK);

If you are constructing the array you're attempting to json_encode with your code, then you should be able to simply add ints rather than numeric strings into the array as you build it:

$data['eCode'] = 0;         // use 0 instead of '0'$data['city'] = 'london';$data['time'] = '15:35';

However, if the array you're working with is coming from some other source, such as a query result, you'll need to decide between several options:

  1. Use JSON_NUMERIC_CHECK when you json_encode (keep in mind that it won't only convert that column to a number; it will convert anything that looks like a number - you can see various warnings about that in the comments on this answer and on the PHP documentation)

  2. Figure out how to make adjustments to the data source or the way you fetch the data so that the array you get will already have ints instead of strings (for example, turning off emulated prepares in PDO)

  3. Convert the value in the array before you json_encode. There are various ways to do that, $data['eCode'] = (int) $data['eCode'];, for example.

In my opinion, option 2 would be the best way to handle it.