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

前端 未结 8 901
礼貌的吻别
礼貌的吻别 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 17:00

    This function will cast the correct data type for the JSON output and utf8_encode the strings.

        /* Change data-type from string to integar or float if required.
         * If string detected then utf8_encode() it. */
        function cast_data_types ($value) {
          if (is_array($value)) {
            $value = array_map('cast_data_types',$value);
            return $value;
          }
          if (is_numeric($value)) {
            if(strpos('.', $value)===false) return (float)$value;
            return (int) $value;
          }
          return utf8_encode((string)$value);
        }
    
    json_encode (cast_data_types($data));
    

提交回复
热议问题