2012-03-14 57 views
0

想要在数组中的每个项目上使用iconv()并将它们存储到不同的数组,然后将新数组重新分配给旧数组。这是做到这一点的正确方法吗?PHP循环数组和iconv每个项目

foreach($partinfo as $key=>$item) { 
    $p[$key] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $item); 
} 
$partinfo = $p; 
+0

看起来没问题。为'$ p'变量添加init:'$ p = array();'。运行并测试。 – kirilloid 2012-03-14 20:15:47

+0

您可以使用[带引用的foreach](http://php.net/manual/en/control-structures.foreach.php)使这更容易。 – cmbuckley 2012-03-14 20:19:30

回答

0

您可以通过使用mb_detect_encoding得到一个字符串的编码:

mb_detect_encoding($str, 'UTF-8', true); 

来源:http://php.net/manual/en/function.mb-detect-encoding.php

你的循环后,只写了另一个测试循环,通过去,又呼应了返回值和你自己看。