Change array by reference

I get a multidimensional array from my MySQL database

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 the conversion of ISO-8859-1 to UTF8, which can be done using the "do-it-yourself" 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 does not save the changes (when I return the value of $ value, it works, but the modification is not saved at the end ...)

EDIT: This is the loadtext function that I undertake to use (in fact, I haven't done 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; } 
+6
source share
3 answers

You can try the following:

 $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 even better, you could try using the MySQL CONVERT() function in your query so that the rows you return are already in UTF8 format.

http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html

At least use PHP mb_convert_encoding() instead of your home function. There is no reason to reinvent the wheel.

http://jp2.php.net/manual/en/function.mb-convert-encoding.php

+6
source

I found a simple answer myself, which works very well for me, 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); 
+2
source

It occurred to me that there is another solution to this particular problem, which avoids the problem of changing the array by reference in general.

 $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 that are only available with PHP 5.3, so if you have something old, you will have to implement it differently and this may not cost problems, but I think this is a pretty good way.

In addition, it may be more efficient / look cleaner to do it as follows:

 $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()); 
0
source

Source: https://habr.com/ru/post/916972/


All Articles