[php] 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 ?

Best.

This question is related to php arrays utf

The answer is


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.


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.

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

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)

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

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

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');
}
);
?>

There is an easy way

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

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

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.


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.


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

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