PHP array encoding conversion example

怪我咯
Release: 2023-03-13 15:50:01
Original
1822 people have browsed it

In PHP programming, operations on arrays sometimes encounter problems related to encoding conversion.

Because of the display effect of some special characters, UTF-8 was changed to GBK. Due to the use of ajax technology, an old problem was involved - encoding conversion.

SomeForm validationneeds to return json data. PHP’s json_encode function only supports utf-8 encoding. I have no choice but to iconv. The desired effect is to convert the GBK array into a utf-8 array for transmission. to the json_encode function.

This is how it was initially done. After serializing the array, use the iconv function to convert the encoding, and then deserialize it again:

The code is as follows:

unserialize(iconv('gbk','utf-8',serialize($array)));
Copy after login

The result obtained is blank , then I remembered that the default encoding ini_set('default_charset', 'gbk'); was set in Configuration file; It is definitely not easy to use gbk to deserialize utf-8 strings. Here is the sequence It should be possible to add an ini_set('default_charset', 'utf-8'); between deserialization and deserialization, but it always feels a bit awkward to do so because it is a global encoding setting and can easily lead to encoding problems in other places. , such as Database Operation. Then change your mind and use the serialization method of constructing the array prototype, with the help of the var_export function, the final function is as follows:

The code is as follows:

function array_iconv($in_charset,$out_charset,$arr){  
        return eval('return '.iconv($in_charset,$out_charset,var_export($arr,true).';'));  
}
Copy after login

The principle is very simple var_export sets the second parameter to true and returns Array prototype string, convert the string to utf-8 encoding, and then use eval to perform the return (similar to anonymous function?), which perfectly solves the problem.

Follow-up: Later, I searched for information on the Internet to see if there is a better method. All I found are similar. They all use recursive calls to iconv. If there are too many array elements or more dimensions, the performance will suffer. It's definitely not that good. What's better is the native code method. You don't need to consider whether it is an N-dimensional array or an associative array. Everything has been automatically completed to ensure that the data is consistent before and after the array conversion. Judging from the length of the code and the comparison between loops and native methods, I believe everyone already has a choice.

The above is the detailed content of PHP array encoding conversion example. For more information, please follow other related articles on the PHP Chinese website!

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!