Coder Perfect

PHP’s json encode function converts numbers to strings.


The PHP json encode function is giving me trouble. It encrypts numbers as strings, for example.

array('id' => 3)


"{ ["id": "3", ...)

When these values are encountered by JavaScript, they are interpreted as strings, and numeric operations fail on them. Is there a way to stop json encode from converting numbers to strings? Thank you very much!

Asked by Chris Barnhill

Solution #1

Note that a flag for auto-converting numbers has been available since PHP 5.3.3 (the options argument was added in PHP 5.3.0):

$arr = array( 'row_id' => '1', 'name' => 'George' );
echo json_encode( $arr, JSON_NUMERIC_CHECK ); // {"row_id":1,"name":"George"}

Answered by Rijk

Solution #2

I was reading from a database (PostgreSQL) as well, and everything was a string. To build up our final results array, we loop over each row and perform things with it, thus I used

$result_arr[] = array($db_row['name'], (int)$db_row['count']);

to compel it to be an integer number within the loop When I use json encode($result arr), it now formats it correctly as an integer. This allows you to decide what counts as a number from your database and what doesn’t.


The JSON NUMERIC CHECK flag as a second argument to the json encode() function can also be used to achieve this on the fly. However, as indicated in this user example in the documentation (copied below), you must exercise caution when utilizing it:

// International phone number
json_encode(array('phone_number' => '+33123456789'), JSON_NUMERIC_CHECK);

After that, you’ll get this JSON:


Answered by mouckatron

Solution #3

I conducted a brief test:

$a = array(
    'id' => 152,
    'another' => 'test',
    'ananother' => 456,
$json = json_encode($a);
echo $json;

If I’m not mistaken, this appears to be what you describe.

And this is what I get as a result:


So, in this case, the integers have not been converted to string.

Still, it’s possible that this is dependent on the PHP version we’re using: depending on the PHP version, a handful of json encode-related problems have been fixed…

This test was done using PHP 5.2.6; I’m getting the same results with PHP 5.2.9 and 5.3.0; unfortunately, I don’t have another 5.2.x version to test with:-(

Which PHP version are you using? Is your test-case more complicated than the one you provided?

Perhaps one of the bug reports on is related? Bug #40503: json encode integer conversion is inconsistent with PHP, for example?

Bug #38680, by the way, might pique your attention as well.

Answered by Pascal MARTIN

Solution #4

$arr = array(‘var1′,’var2′,’var3′,’var4′,’var5′,’var6′,’var7′,’var8′,’var9′,’var10’ $arr, JSON NUMERIC CHECK); $json = json encode($arr, JSON NUMERIC CHECK);

However, it is only compatible with PHP 5.3.3. See the PHP json encode change log at

Answered by habibillah

Solution #5

The identical issue is happening to me (PHP-5.2.11/Windows). This is the workaround I’m utilizing.

$json = preg_replace( "/\"(\d+)\"/", '$1', $json );

All (non-negative, integer) numbers wrapped in quotation marks are replaced with the number itself (‘”42″‘ becomes ’42’).

This note in the PHP manual is also worth mentioning.

Answered by oli_arborum

Post is based on