json_encode()将大量数字转换为科学计数法

时间:2018-08-04 18:36:49

标签: php json scientific-notation

我正在接收json对象数组(下面的示例)。我需要遍历它们并分别输出json对象。值“ MessageID”之​​一很大,当我运行json_encode()时,它会以科学计数法进行编码。这对我来说不起作用,因为数字是数据库密钥。

这是我的代码:

<?php
$json = file_get_contents('php://input');
$events = json_decode($json);

foreach($events as $event){
  $debug = var_export($event, true); //still see the whole number here!
  $event_json = json_encode($event); //number is converted to scientific notation
  //code to create records in database
}
?>

示例输入json:

[{
   "event": "open",
   "time": 1433103519,
   "MessageID": 19421777396190490,
   "email": "api@mailjet.com",
   "mj_campaign_id": 7173,
   "mj_contact_id": 320,
   "customcampaign": "",
   "CustomID": "helloworld",
   "Payload": "",
   "ip": "127.0.0.1",
   "geo": "US",
   "agent": "Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko Firefox/11.0"
}]

我尝试插入以下内容以将数字更改为字符串,但无效。

$event["MessageID"]=(string)$event["MessageID"]

我该如何将MessageID属性转换为字符串,或者还有另一种方法来使它以与输入时相同的方式输出?

0 个答案:

没有答案