php数组编码转换小例子

WBOY
Release: 2016-07-27 16:56:17
Original
893 people have browsed it
  1. function array_iconv($in_charset,$out_charset,$arr){
  2. return eval('return '.iconv($in_charset,$out_charset,var_export($arr,true).';'));
  3. }
复制代码

原理分析: var_export设置第二个参数为true,返回数组原型字符串,将字符串转换为utf-8编码,之后再用eval来执行返回(类似匿名函数?),至此完美解决问题。

后续:网上找到的方法,大多是利用递归调用iconv的方式,如果数组元素过多或者维数多一些,性能上肯定不怎么样了,更好的是原生代码的方式,不需要考虑是N维数组还是关联数组,一切都已经自动完成,保证数组转换前后数据一致。

从代码的长短以及循环和原生方法的比较上,还是本文的方法不错哦。



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!