JSON_NUMERIC_CHECK and phone numbers

json_numeric_check laravel
json number
php json_encode utf8
php json_encode array
json_unescaped_slashes not working php
php json_encode pretty
php json_encode single quotes

Our PHP API outputs results using json_encode with JSON_NUMERIC_CHECK enabled, which is important for financial figures, binary values etc.. but we have recently introduced a phone number field, and it is removing zeros from the beginning of the number when it casts it as an integer.

I've tried to force "(string)" type on the value before it goes to json_encode, but it looks like the function is overriding it. eg:

$output = array(
    'is_bool' => 1,
    'total_amount' => '431.65',
    'phone_number' => (string) '0272561313'
echo json_encode($output,JSON_NUMERIC_CHECK);



Any suggestions on how to best handle this would be much appreciated. I really don't want to have to resort to adding trailing spaces at the end of the phone number when it's output just to get it through as a string.


Instead of typecasting it to a string do something like this:

$output = array(
    'is_bool' => 1,
    'total_amount' => '431.65',
    'phone_number' => '"0272561313"' // add quotations

echo '<pre>';

It'l keep the trailing zero:

    "is_bool": 1,
    "total_amount": 431.65,
    "phone_number": "\"0272561313\""


$test = json_decode($test, true);


    [is_bool] => 1
    [total_amount] => 431.65
    [phone_number] => "0272561313"

php JSON_NUMERIC_CHECK and phone numbers?, $test = json_encode($output,JSON_NUMERIC_CHECK Had the same issue with phone numbers, this walks over an array (no objects!) to cast only numeric  Type Any Phone & Search Free! Search Name, Address, Age, Relatives, Marital Status & More. Search Name, Address, Age & More Personal Data. Award Winning Public Data, Search Free Now

You can try this:

  "is_bool": 1,
  "total_amount": 431.65,
  "phone_number": "xn#0272561313"

as example in mysql: SELECT CONCAT('xn#', phone_number) as phone_number...

If you used json_encode(), replace the string like this:

echo str_replace('xn#','',$your_encoded_json);

Guide on PHP json_decode: Learn json_encode and Other Functions, () . PHP arrays are not supported by XML but can be converted into JSON. Get Name, Phone Numbers, Emails, Address, Public Records, & More

Had the same issue with phone numbers, this walks over an array (no objects!) to cast only numeric values not starting with 0 or + to an int or float.

        array_walk_recursive($json, function (&$value, $key)
            if(is_string($value) && is_numeric($value))
                // check if value doesn't starts with 0 or +
                if(!preg_match('/^(\+|0)/', $value))
                    // cast $value to int or float
                    $value   += 0;

        return json_encode($json);

PHP, () function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable. It only works with UTF-8 encoded strings. assoc: It is a boolean variable. If it is true then objects returned will be converted into associative arrays. Easily and fast to find a lost phone etc. Find a cell phone now!

pass the data to this function to get JSON encoding with + and 0 preceded data staying as a string. I am using double encoding and str_replace to get over the issue.

public static function json_encode($data){
    $nonnumeric = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    preg_match_all("/\"[0\+]+(\d+)\"/",$nonnumeric, $vars);
    foreach($vars[0] as $k => $v){
        $numeric = preg_replace("/\:\s*{$vars[1][$k]},/",": {$v},",$numeric);
    return $numeric;

PHP, ( $value, $option, $depth ) Parameters: $value: It is a mandatory parameter which defines the value to be encoded. Therefore, JSON Schema can not use type alone to distinguish between integers and non-integers. The JSON Schema specification recommends, but does not require, that validators use the mathematical value to determine whether a number is an integer, and not the type alone. Therefore, there is some disagreement between validators on this point.

You no need to use


Instead use attribute-casting


    class SomeModel extends Model
        protected $casts = [
            'field_name' => 'array',
 'status' => 'boolean'

Predefined Constants - Manual, (integer) Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty. Post Link : Check If Variable Is A Number In JavaScript . This tutorial explains how to check variable is a number in javascript. Lets see the below example, where we have used typeof operator and isNaN() function to verify the variable is number or not. typeof – If variable is a number, it will returns a string named "number".

json_encode - Manual, 5.5.0, E_WARNING is no longer emitted for an invalid value . 5.5.0, The json_encode($numbers, JSON_NUMERIC_CHECK) ); International phone number I initially thought this was HHVM not supporting JSON_NUMERIC_CHECK constant used as option in json_encode (and Laravel's models use this by default: Numeric strings to number type). But after testing this using a vanilla PHP script:

PHP: How to json_encode numbers properly, <?php echo json_encode( $data , JSON_NUMERIC_CHECK); ?> It also is dangerous if you have a phone number with +, e.g. +3712345678  json_decode AND json_encode long integers without loosing data Tag: php , json , parsing , numbers , bigint As noted in the PHP documentation, when json_decode ing a data structure containing long integers, they'll be converted to floats.

PHP, I'm returning from a PHP query result using json_encode since my data are numbers, I use the JSON_NUMERIC_CHECK flag to ensure that  SharePoint Online Column Formating with Json currently not provide operation to remove commas from number field. A workaround is to create a Calculated column with formula: =TEXT(yournumber, "0") Then the commas in the number field will gone into the Calculated column: Thanks. Best Regards