I receive from my MySQL database a multidimensional array
Array
(
[0] => Array
(
[page] => categorypropose
[value] => baby-sitters
[id] => 357960
)
[1] => Array
(
[page] => categorysearch
[value] => adéquate pour garder
[id] => 357961
)
...
)
In this array, I have some ISO-8859-1 to UTF8 conversion to do via a 'homemade' function "loadtext".
But when I do this :
$array = $query->result_array();
foreach($array as &$k)
{
foreach ($k as &$value)
{
//Works
$value = $this->loadtext($value, 'ISO-8859-1');
}
}
//Back to normal as $this->loadtext never existed
print_r($array);
It doesn't conserve the changes (When I echo $value, it works, but the modification is not kept at the end ...)
EDIT : This is the function loadtext that I am oblige to use (actually, I didn't make it but I have to use it ...)
function loadtext($text,$charset){
$text = stripslashes($text);
if($charset!="UTF-8")
$text = iconv("UTF-8",$charset,$text);
$text = str_replace(" :"," :",$text);
$text = str_replace(" ;"," ;",$text);
$text = str_replace(" !"," !",$text);
$text = str_replace(" ?"," ?",$text);
$text = str_replace(" ."," .",$text);
$text = str_replace(" …"," …",$text);
return $text;
}
You could try it like this:
$array = $query->result_array();
foreach($array as &$k)
{
foreach ($k as $i => &$value)
{
//Works
$k[$i] = $this->loadtext($value, 'ISO-8859-1');
}
}
//Back to normal as $this->loadtext never existed
print_r($array);
But better yet, you could try using the MySQL function CONVERT()
in your query so that the strings you get back are already in UTF8 format.
http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html
At the very least, use PHP's mb_convert_encoding()
instead of your homemade function. There's no reason to reinvent the wheel.
http://jp2.php.net/manual/en/function.mb-convert-encoding.php
I found a simple answer myself which works very wellfor me bur using another method in php to change the value i get from mysql result
// ur array from mysql
$array = $query->result_array();
//try it works 100 % for me just one line of code to modify
$result= iconv('UTF-8', 'ASCII//TRANSLIT',$array);
source : php.net
// or if doesnt work then u can try like this to modify u can put it inside a foreach loop where you are loopin values
$page = array['page']; // to acces that element in the array where to modify
$result= iconv('UTF-8', 'ASCII//TRANSLIT',$page);
It occurred to me that there's another solution to this particular problem that avoids the issue of modifying an array by reference altogether.
$array = array_map(function ($row) {
return array_map(function ($col) { return mb_convert_encoding($col, 'ISO-8859-1'); }, $row);
}, $query->result_array());
This uses anonymous functions which are only available since PHP 5.3 so, if you have something older, you'll have to implement it differently and it might not be worth the trouble but I think it's a pretty good way to go.
Also, it may be more efficient/look cleaner to do it like this:
$colFn = function ($col) { return mb_convert_encoding($col, 'ISO-8859-1'); };
$rowFn = function ($row) use ($colFn) { return array_map($colFn, $row); };
$array = array_map($rowFn, $query->result_array());