Json_decode integer overflow solution. This is a bit like if the mysqlint type is exceeded, and we only need to use bigint, so this type of problem may also occur due to the int data range in php. Encoding is a bit like overflow when the mysql int type is exceeded, and we only need to use bigint type. this problem may also occur due to the int data range in php.
An error occurs during encoding, that is, when processing json, the int value with a large value is damaged after decoding, for example:
| The code is as follows: |
|
|
$ Array = array (
"Id1" => 2147483647,
"Id2" = & gt; 2147483648
);
$ Json = json_encode ($ array );
$ Out = json_decode ($ json, true );
Var_dump ($ out );
Theoretically, we can see that:
Array (2 ){
["Id1"] = & gt; int (2147483647)
["Id2"] = & gt; int (2147483648)
} |
But on my computer, I actually got:
| The code is as follows: |
|
Array (2 ){
["Id1"] = & gt; int (2147483647)
["Id2"] => int (-2147483646)
}
|
This is determined by the PHP integer range, which depends on the operating system. In a 32-bit operating system, the maximum integer value of PHP is 2147483647. you can see it by outputting PHP_INT_MAX.
In general, if you assign a large value, PHP will automatically determine the range of the value and automatically convert the type, such:
| The code is as follows: |
|
$ Large_number = 2147483647;
Var_dump ($ large_number); // int (2147483647)
$ Large_number = 2147483648;
Var_dump ($ large_number); // float (2147483648)
$ Million = 1000000;
$ Large_number = 50000 * $ million;
Var_dump ($ large_number); // float (50000000000) |
However, this check is not performed in the json_decode method. this is a bug in PHP (earlier version). in versions later than 5.3, this problem does not exist.
If you do not want to update your PHP, you can convert the number into a string. Take the above code as an example:
| The code is as follows: |
|
$ Array = array (
"Id1" => 2147483647,
"Id2" = & gt; 2147483648
);
$ Json = json_encode ($ array );
$ Json = preg_replace ('/("idd" :) (d {9,})/I', '$ {1} "$ {2}"', $ json );
$ Out = json_decode ($ json, true );
Var_dump ($ out );
|
Of course, how to replace this is on demand and requires careful testing.
When the struct int type is exceeded, it only needs to use the bigint type. This type of problem may also occur due to the int data range in php. Encoding...