php - 序列化为 JSON 时 PHP 中的意外行为
问题描述
PHP 7.3.15 拉拉维尔 5.8.38
dump(ini_get('serialize_precision'));
// Out: "-1"
$data = DB::table('SOME QUERY HERE')->get();
dump($data);
#items: array:10 [
0 => {#713
+"creation": "2020-04-23 04:00:00"
+"user_name": "Some Client"
+"amount": 17.8
+"count": 3
}
dump(json_encode($data, JSON_PRETTY_PRINT));
[
{
"creation": "2020-04-23 04:00:00",
"user_name": "Some Client",
"amount": 17.799999999999997 // Why?
"count": 3
}
我在这里做错了什么?当我将 JSON 格式的数据发送到我的 Javascript 客户端时,我需要同样的精度。
提前致谢。
更新 我已经这样做了:
ini_set("serialize_precision", -1);
但这有效:
dump(json_encode([1002.31, 2002.42]));
// Out: "[1002.31,2002.42]"
似乎问题出在序列化 Laravel 集合时?
解决方案
推荐阅读
- javascript - 使用 withRouter 和 connect,如何在酶中断言 history.push 被调用?
- ios - 无法在 iOS 13.5.1 上运行应用程序
- assembly - SBCL 优化:我们可以为位向量编译一个有效的人口计数吗?
- r - geom_point 不接受 0 作为值
- javascript - 我如何在不使用任何服务器的情况下在 javascript 中导入 json 文件?
- firebase - 无法从函数中获取价值,即使我有一个非常相似的函数。- 颤振
- python - 基本 Selenium 项目无效参数错误
- python - 使用相同的 bin 有效地对多个数组进行 bin 划分
- python - 登录后,我的下一个 class.show() 没有出现
- html - 在 HTML 注释标签中放置 HTML 注释标签