dongshi1934 2014-06-21 22:52
浏览 275
已采纳

PHP有json_encode的十进制数字

I have an issue with some json code where decimal numbers MUST be encoded without quotes and maintain two decimal places

e.g.

{12.34, 33.40, 25.00}

My problem is that the array I have creates the numbers as string

foreach($numbers as $n)
{
  $result[] = number_format($n, 2, '.', '');
}
json_encode($result);

// result = {"12.34", "33.40", "25.00"}
  • 写回答

4条回答 默认 最新

  • duannuochi3549 2019-02-20 09:38
    关注

    I had similar issie with this. This may not be the best code but it work for me. Maybe it can help you. Try this (I am using codeigniter):

    function getData() {
    
        $data = $this->Teplomer_model->showData(); //get your data from database as return $query->result();
    
        //create array
        $arr = array();
    
        //foreach data to array
        foreach ($data as $row) {
            $arr[] = array(
            "id"                =>  $row->id_teplota,
            "datum"             =>  $row->cas_teplota,
            "teplota"           =>  $row->teplota_teplota,
            "vlhkost"           =>  $row->vlhkost_teplota
            );
        }
    
        //echo array as json and check if there is any numbers
        echo json_encode($arr, JSON_NUMERIC_CHECK );
    }
    

    And output:

    {"id":3,"datum":"2019-02-08 14:03:31","teplota":22.33,"vlhkost":19.7},{"id":4,"datum":"2019-02-08 14:18:35","teplota":23,"vlhkost":19}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么