PHP array encoding conversion small example

WBOY
Release: 2016-07-27 16:56:17
Original
982 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. }
Copy code

Principle analysis: var_export sets the second parameter to true, returns the array prototype string, converts the string to utf-8 encoding, and then uses eval to perform the return (similar to an anonymous function?), which perfectly solves the problem.

Follow-up: Most of the methods found on the Internet use recursive calling iconv. If the array has too many elements or more dimensions, the performance will definitely not be good. The better method is the native code method, and there is no need to consider N Whether it is a dimensional array or an associative array, everything has been completed automatically to ensure that the data is consistent before and after the array conversion.

From the length of the code and the comparison between the loop and the native method, the method in this article is still good.



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!