Feeds:
Posts
Comments

Posts Tagged ‘PHP’

PHP: How to json_encode numbers properly

Here is a handy tip about PHP’s json_encode function.

In some cases numeric data may ¬†appear as numeric strings, for example 1 as “1”. While consuming the json data on a client app that would mean an extra parsing to get for example an int from the string.

The solution is to simply use an extra option (JSON_NUMERIC_CHECK) on the json_encode function call:

<?php echo json_encode($data, JSON_NUMERIC_CHECK); ?>

I have to point out that this option requires a PHP version of 5.3.3 or higher.

Advertisements

Read Full Post »