JSON_NUMERIC_CHECK and phone numbers

前端 未结 7 1129
执念已碎
执念已碎 2021-01-11 18:45

Our PHP API outputs results using json_encode with JSON_NUMERIC_CHECK enabled, which is important for financial figures, binary values etc.. but we have recently introduced

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-11 19:19

    works for me :

    // $DATAS -> datas returned by the server
    $DATAS = array(
        array('tel' => '0606060606'), 
        array('tel' => '0707070707') 
    );
    
    foreach($DATAS as $k => $v){
    
        $tel = " ".$v['tel']." ";  // ADD WHITE SPACE
        $DATAS[$k]['tel'] = $tel;
    }
    
    echo json_encode($DATAS, JSON_NUMERIC_CHECK);
    

    // output : [{"tel":" 0606060606 "},{"tel":" 0707070707 "}]

    Maybe define css white space or trim datas in the json encoded for obtain the perfect parsed number

提交回复
热议问题