[英]How to convert php array to utf8?
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.最好。
$utfEncodedArray = array_map("utf8_encode", $inputArray );
完成工作并返回一个带有数字键的序列化数组(不是关联)。
array_walk(
$myArray,
function (&$entry) {
$entry = iconv('Windows-1250', 'UTF-8', $entry);
}
);
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");
There is an easy way有一个简单的方法
array_walk_recursive(
$array,
function (&$entry) {
$entry = mb_convert_encoding(
$entry,
'UTF-8'
);
}
);
You 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');
}
);
?>
You can send the array to this function:您可以将数组发送到此函数:
function utf8_converter($array){
array_walk_recursive($array, function(&$item, $key){
if(!mb_detect_encoding($item, 'utf-8', true)){
$item = utf8_encode($item);
}
});
return $array;
}
It works for me.这个对我有用。
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)
You can use string utf8_encode( string $data )
function to accomplish what you want.您可以使用
string utf8_encode( string $data )
函数来完成您想要的。 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.
您可以编写自己的函数,使用该函数可以在 utf8_encode 函数的帮助下转换数组。
Due to this article is a good SEO site, so I suggest to use build-in function " mb_convert_variables " to solve this problem.由于这篇文章是一个不错的SEO站点,所以我建议使用内置函数“ mb_convert_variables ”来解决这个问题。 It works with simple syntax.
它使用简单的语法。
mb_convert_variables('utf-8', 'original encode', array/object)
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;
}
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.这会将任何字符集转换为 UTF8 并保留数组中的键。 So instead of "lazy" converting each row using
array_walk
, you could do the whole result set in one go.因此,您可以一次性完成整个结果集,而不是使用
array_walk
来“懒惰”地转换每一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.