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

It says in the json_encode() documentation:

This function only works with UTF-8 encoded data.

You should convert it to utf-8 with iconv or mbstring first.


As Greg mentioned, I had to encode åäö to UTF-8. But I did't use iconv or mbstring. When I utf8_encode() all values before putting the values to the array the problem was solved.


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