Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify an array by reference

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;
}
like image 878
Kalzem Avatar asked May 30 '12 04:05

Kalzem


2 Answers

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

like image 176
Okonomiyaki3000 Avatar answered Nov 04 '22 20:11

Okonomiyaki3000


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);          
like image 33
Rinzler Avatar answered Nov 04 '22 19:11

Rinzler