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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度