Reputation: 231
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.
Upvotes: 23
Views: 136236
Reputation: 767
<?php
$search = [
"\x5A\x6F\xEB"
];
$searchRes = array_map('utf8_encode', $search);
var_dump($searchRes);
echo PHP_EOL.PHP_EOL;
$searchRes = array_map(fn($item) => mb_convert_encoding($item, 'UTF-8', 'ISO-8859-1'), $search);
var_dump($searchRes);
Upvotes: 0
Reputation: 171
There is an easy way
array_walk_recursive(
$array,
function (&$entry) {
$entry = mb_convert_encoding(
$entry,
'UTF-8',
'WINDOWS-1250'
);
}
);
Upvotes: 17
Reputation: 1362
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
Upvotes: 0
Reputation: 852
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');
}
);
?>
Upvotes: 11
Reputation: 99
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.
Upvotes: 9
Reputation: 21
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)
Upvotes: 2
Reputation: 5923
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;
}
Upvotes: 0
Reputation: 31
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)
Upvotes: 0
Reputation: 2643
$utfEncodedArray = array_map("utf8_encode", $inputArray );
Does the job and returns a serialized array with numeric keys (not an assoc).
Upvotes: 48
Reputation: 171
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");
Upvotes: 17
Reputation: 1528
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.
Upvotes: -3
Reputation: 212522
array_walk(
$myArray,
function (&$entry) {
$entry = iconv('Windows-1250', 'UTF-8', $entry);
}
);
Upvotes: 19
Reputation: 4268
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.
Upvotes: 0