23

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.

user2369594
  • 231
  • 1
  • 2
  • 5

12 Answers12

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

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

Max
  • 2,561
  • 1
  • 24
  • 29
  • Works very well for me! :) –  Oct 18 '20 at 03:39
  • 2
    `utf8_encode() expects parameter 1 to be string, array given`; PHP 7.4.4. – John Dec 17 '20 at 04:03
  • 1
    Just tested it with 7.4.3 - no complaints - are you sure your input array doesn't contain any array elements? This solution is not recursive! – Max Dec 18 '20 at 14:21
19
array_walk(
    $myArray,
    function (&$entry) {
        $entry = iconv('Windows-1250', 'UTF-8', $entry);
    }
);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
17

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");
Torsten
  • 171
  • 1
  • 2
16

There is an easy way

array_walk_recursive(
  $array,
  function (&$entry) {
    $entry = mb_convert_encoding(
        $entry,
        'UTF-8',
        'WINDOWS-1250'
    );
  }
);
Toto
  • 2,329
  • 22
  • 40
phpdev
  • 161
  • 1
  • 5
  • The 3rd parameter of `mb_convert_encoding()` should be specified otherwise it will take the value of `mbstring.internal_encoding` or `default_charset` (php.ini) which may not be desired encoding. – Toto Apr 10 '23 at 21:09
11

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');
}
);
?>
John
  • 1
  • 13
  • 98
  • 177
Srihari Goud
  • 824
  • 10
  • 25
9

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.

Worthwelle
  • 1,244
  • 1
  • 16
  • 19
Mr. Pancho
  • 99
  • 1
  • 4
2

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)
0

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.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
0

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)

Jerry Chen
  • 31
  • 1
  • 5
0

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;
}
Sebastian Viereck
  • 5,455
  • 53
  • 53
0

The simple way tha works is:

array_map(callable $callback, array $array1, array $... = ?): array

//Example #1 Example of the function array_map()

<?php
function cube($n)
{
    return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>

The Output of $b is:

Array
(
    [0] => 1
    [1] => 8
    [2] => 27
    [3] => 64
    [4] => 125
)

For futher details src -> PHP: array_map - Manual

Caique Andrade
  • 1,025
  • 7
  • 9
-3

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.

Josh Stuart
  • 1,530
  • 1
  • 13
  • 23