json_encode() non utf-8 strings?

后端 未结 6 1174
名媛妹妹
名媛妹妹 2020-11-30 06:54

So I have an array of strings, and all of the strings are using the system default ANSI encoding and were pulled from a SQL database. So there are 256 diffe

6条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 07:06

    I found the following answer for an analogous problem with a nested array not utf-8 encoded that i had to json encode:

    $inputArray = array(
        'a'=>'First item - à',
        'c'=>'Third item - é'
    );
    $inputArray['b']= array (
              'a'=>'First subitem - ù',
              'b'=>'Second subitem - ì'
        );
     if (!function_exists('recursive_utf8')) {
      function recursive_utf8 ($data) {
         if (!is_array($data)) {
            return utf8_encode($data);
         }
         $result = array();
         foreach ($data as $index=>$item) {
            if (is_array($item)) {
               $result[$index] = array();
               foreach($item as $key=>$value) {
                  $result[$index][$key] = recursive_utf8($value);
               }
            }
            else if (is_object($item)) {
               $result[$index] = array();
               foreach(get_object_vars($item) as $key=>$value) {
                  $result[$index][$key] = recursive_utf8($value);   
               }
            } 
            else {
               $result[$index] = recursive_utf8($item);
            }
         }
         return $result; 
       }
    }
    $outputArray =  json_encode(array_map('recursive_utf8', $inputArray ));
    

提交回复
热议问题