PHP JSON大整数 [英] PHP JSON large integers

查看:142
本文介绍了PHP JSON大整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用json_encode将我的php多维数组转换为输出json.通常,此函数会将所有值转换为字符串.为了确保将整数值作为整数值发送到javascript,我正在使用数字检查:

I am using json_encode to transform my php multidimensional array to output json. Normally, this function would convert all values to strings. To make sure that integers values are send to javascript as integer values, I am using the numeric check:

$json = json_encode($data, JSON_NUMERIC_CHECK);

对于我的应用程序,除了一种情况,这在所有情况下都可以正常工作.在php数组(从数据库中提取)中,有一个字段包含非常大的整数.我将其另存为VARCHAR,但不幸的是,在编码为json时,该值转换为整数.问题在于,由于这是一个非常大的整数,因此会四舍五入,因此不代表真实值.我该如何解决这个问题?

This works fine in all but one case for my app. In the php array (which is extracted from the database), there is one field that contains very large integers. I save it to database as a VARCHAR, but unfortunately this is converted to an integer when encoding to json. The problem is that since this is a very large integer, it gets rounded and therefore does not represent the true value. How could I tackle this problem?

推荐答案

也许是来晚了,但我确实遇到了同样的问题,由于必须在该版本上运行的旧代码,因此卡在服务器上的PHP 5.3上.我使用的解决方案很笨,但是对我有用:在从数据库读取的varchar长整数的末尾添加一个空格字符,然后再将其发送到使用JSON_NUMERIC_CHECK进行json编码.

Maybe is to late but i did hit the same problem, and stuck on PHP 5.3 on the server because of legacy code that must be run with that version. The solution that i used is dumb, but did work for me: simple add an space character at the end of the long integer that is varchar readed from the db, and before sending it to json encode with JSON_NUMERIC_CHECK.

这篇关于PHP JSON大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆