如何将php数组转换为utf8?

use*_*594 16 php arrays utf

我有一个数组:

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);
Run Code Online (Sandbox Code Playgroud)

我想将它从windows-1250转换为utf-8,因为我有像 这样的字符

最好.

Max*_*Max 34

$utfEncodedArray = array_map("utf8_encode", $inputArray );
Run Code Online (Sandbox Code Playgroud)

作业并返回带有数字键(不是assoc)的序列化数组.

  • `utf8_encode() 期望参数 1 为字符串,给定数组`; PHP 7.4.4。 (2认同)

Mar*_*ker 18

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


小智 13

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

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


小智 11

有一个简单的方法

array_walk_recursive(
  $array,
  function (&$entry) {
    $entry = mb_convert_encoding(
        $entry,
        'UTF-8'
    );
  }
);
Run Code Online (Sandbox Code Playgroud)


Mr.*_*cho 9

您可以将数组发送到此函数:

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;
}
Run Code Online (Sandbox Code Playgroud)

这个对我有用。


Sri*_*oud 8

你可以使用这样的东西

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


Viv*_*adh 0

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