【问题标题】:Modify an array by reference通过引用修改数组
【发布时间】:2012-06-04 07:55:55
【问题描述】:

我从我的 MySQL 数据库收到一个多维数组

Array
(
[0] => Array
    (
        [page] => categorypropose
        [value] => baby-sitters
        [id] => 357960
    )

[1] => Array
    (
        [page] => categorysearch
        [value] => adéquate pour garder
        [id] => 357961
    )
...
)

在这个数组中,我通过“自制”函数“loadtext”进行了一些 ISO-8859-1 到 UTF8 的转换。

但是当我这样做时:

    $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);

它不会保存更改(当我回显 $value 时,它​​可以工作,但最后不会保留修改...)

编辑:这是我必须使用的函数 loadtext(实际上,我没有做到,但我必须使用它......)

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;
}

【问题讨论】:

  • 我会添加一些更多的调试内容 - print_r 和“外部”循环中的另一个。并测试你是否只是做$array = array(array(1, 2, 3), array(4, 5, 6))$value = $value * 2 来测试。

标签: php mysql arrays reference multidimensional-array


【解决方案1】:

你可以这样尝试:

$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);

但更好的是,您可以尝试在查询中使用 MySQL 函数 CONVERT(),以便返回的字符串已经是 UTF8 格式。

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

至少,使用 PHP 的 mb_convert_encoding() 代替您自制的函数。没有理由重新发明轮子。

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

【讨论】:

  • 谢谢我用了你的方法。而且我不知道 MySQL 有 CONVERT() 函数!太好了!
  • 接受的答案。零赞成票。应该有一个徽章。
  • 奇怪,我以为我同时这样做了=S。对不起:)
【解决方案2】:

我自己找到了一个简单的答案,这对我来说非常有效,使用 php 中的另一种方法来更改我从 mysql 结果中获得的值

       // 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);

来源: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);          

【讨论】:

  • 在我的 loadtext 函数中,我像这样“$text = str_replace(" :"," :",$text);" 单独操作 $value。顺便说一句,您的第二个代码会保留对 $array 的更改吗?
  • 你应该可以运行代码并告诉我它是否有效吗?我认为 secong 代码应该可以尝试 print_r($array);并退出;调试你得到的结果
  • 奇怪的是它并没有真正起作用 =S。我终于用了御好烧的方法。仍然感谢您的帮助,我 +1 了 ;)
【解决方案3】:

我突然想到,对于这个特定问题还有另一种解决方案,可以完全避免通过引用修改数组的问题。

$array = array_map(function ($row) {
    return array_map(function ($col) { return mb_convert_encoding($col, 'ISO-8859-1'); }, $row);
}, $query->result_array());

这使用了仅在 PHP 5.3 之后才可用的匿名函数,因此,如果您有旧版本,则必须以不同的方式实现它,这可能不值得麻烦,但我认为这是一个很好的方法。

此外,这样做可能更有效/看起来更干净:

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

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 2016-04-02
    • 2013-10-21
    • 1970-01-01
    • 2010-09-23
    • 2015-05-19
    • 2015-04-04
    • 2016-01-26
    • 1970-01-01
    相关资源
    最近更新 更多