Multiple PHP JSON Integers

I am using json_encode to convert my multidimensional php array to json output. Typically, this function converts all values ​​to strings. To make sure that integer values ​​are sent to javascript as integer values, I use a numerical check:

$json = json_encode($data, JSON_NUMERIC_CHECK); 

This works great in all cases except one for my application. In the php array (which is retrieved from the database) there is one field containing very large integers. I save it in the database as VARCHAR, but unfortunately it converts to an integer when encoded in json. The problem is that since it is a very large integer, it is rounded and therefore does not reflect the true value. How could I solve this problem?

+4
source share
2 answers

Do you want a large number to be converted to an integer? Your question makes me believe that you are not doing this. If in this case, remove the JSON_NUMERIC_CHECK parameter from the call and it should not change the encoding of the field.

The documentation for this (and other) constants is here .

0
source

It may be late, but I ran into the same problem and got stuck on PHP 5.3 on the server due to outdated code that must be running with this version. The solution I used is dumb but worked for me: just add a whitespace at the end of a long integer which is a varchar read from db and before sending it to json encode using JSON_NUMERIC_CHECK.

0
source

All Articles