php 如何将php数组转换为utf8?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16688179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:32:50  来源:igfitidea点击:

How to convert php array to utf8?

phparraysutf

提问by user2369594

I have an array:

我有一个数组:

require_once ('config.php');
require_once ('php/Db.class.php');
require_once ('php/Top.class.php');

echo "db";

$db = new Db(DB_CUSTOM);
$db->connect();

$res = $db->getResult("select first 1 * from reklamacje");

print_r($res);

I want to convert it from windows-1250 to utf-8, because I have chars like ?

我想将它从 windows-1250 转换为 utf-8,因为我有像 ?

Best.

最好的事物。

回答by Max

$utfEncodedArray = array_map("utf8_encode", $inputArray );

Does the job and returns a serialized array with numeric keys (not an assoc).

完成工作并返回一个带有数字键的序列化数组(不是关联)。

回答by Mark Baker

array_walk(
    $myArray,
    function (&$entry) {
        $entry = iconv('Windows-1250', 'UTF-8', $entry);
    }
);

回答by Torsten

In case of a PDO connection, the following might help, but the database should be in UTF-8:

在 PDO 连接的情况下,以下可能会有所帮助,但数据库应为 UTF-8:

//Connect
$db = new PDO(
    'mysql:host=localhost;dbname=database_name;', 'dbuser', 'dbpassword',
    array('charset'=>'utf8')
);
$db->query("SET CHARACTER SET utf8");

回答by Srihari Goud

U can use something like this

你可以使用这样的东西

<?php
    array_walk_recursive(
                                $array, function (&$value) {
                                    $value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');
                                }
                        );
?>

回答by phpdev

There is an easy way

有一个简单的方法

array_walk_recursive(
  $array,
  function (&$entry) {
    $entry = mb_convert_encoding(
        $entry,
        'UTF-8'
    );
  }
);

回答by pierre-david Houllé

Previous answer doesn't work for me :( But it's OK like that :)

以前的答案对我不起作用:(但是这样也可以:)

         $data = json_decode(
              iconv(
                  mb_detect_encoding($data, mb_detect_order(), true),
                  'CP1252',
                  json_encode($data)
                )
              , true)

回答by Sebastian Viereck

A more general function to encode an array is:

对数组进行编码的更通用的函数是:

/**
 * also for multidemensional arrays
 *
 * @param array $array
 * @param string $sourceEncoding
 * @param string $destinationEncoding
 *
 * @return array
 */
function encodeArray(array $array, string $sourceEncoding, string $destinationEncoding = 'UTF-8'): array
{
    if($sourceEncoding === $destinationEncoding){
        return $array;
    }

    array_walk_recursive($array,
        function(&$array) use ($sourceEncoding, $destinationEncoding) {
            $array = mb_convert_encoding($array, $destinationEncoding, $sourceEncoding);
        }
    );

    return $array;
}

回答by Vivek Sadh

You can use string utf8_encode( string $data )function to accomplish what you want. It is for a single string. You can write your own function using which you can convert an array with the help of utf8_encode function.

你可以使用string utf8_encode( string $data )函数来完成你想要的。它用于单个字符串。您可以编写自己的函数,使用该函数可以在 utf8_encode 函数的帮助下转换数组。

回答by Jerry Chen

Due to this article is a good SEO site, so I suggest to use build-in function "mb_convert_variables" to solve this problem. It works with simple syntax.

由于这篇文章是一个不错的SEO站点,所以我建议使用内置函数“ mb_convert_variables”来解决这个问题。它使用简单的语法。

mb_convert_variables('utf-8', 'original encode', array/object)

mb_convert_variables('utf-8', 'original encode', array/object)

回答by Josh Stuart

Instead of using recursion to deal with multi-dimensional arrays, which can be slow, you can do the following:

您可以执行以下操作,而不是使用递归来处理可能很慢的多维数组:

$res = json_decode(
    json_encode(
        iconv(
            mb_detect_encoding($res, mb_detect_order(), true),
            'UTF-8',
            $res
        )
    ),
    true
);

This will convert any character set to UTF8 and also preserve keys in your array. So instead of "lazy" converting each row using array_walk, you could do the whole result set in one go.

这会将任何字符集转换为 UTF8 并保留数组中的键。因此array_walk,您可以一次性完成整个结果集,而不是“懒惰”地转换每一行。