How to get json_encode() to work with ISO-8859-1 (åäö)

前端 未结 8 902
礼貌的吻别
礼貌的吻别 2021-01-11 15:58

json_encode() wont work for me when I\'m using åäö. Why? And how can I get it to work?

The php:

echo json_encode($arr);
         


        
8条回答
  •  悲&欢浪女
    2021-01-11 16:53

    Using the standard method when reading from MySQL:

    $resultArray = array();
    while($obj = MySQL_fetch_object($res)) {
     $resultArray[] = $obj;
    }
    $result = json_encode($resultArray);
    

    The encoding can be done using the following:

    $resultArray = array();
    while($obj = MySQL_fetch_object($res)) {
     foreach($obj as $key => $value) {
      if (!is_null($value)) {
       $obj->$key = utf8_encode($value);
      }
     }
     $resultArray[] = $obj;
    }
    $result = json_encode($resultArray);
    

    The if is_null has to be included so that null fields (e.g., DateTime fields) remain null in the output.

提交回复
热议问题