Home > Backend Development > PHP Tutorial > PHP array encoding conversion method reference

PHP array encoding conversion method reference

WBOY
Release: 2016-07-25 08:58:02
Original
1191 people have browsed it
This article introduces some methods of encoding and converting arrays in PHP. Friends in need can refer to it.

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

Due to 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.

Some form verification needs to return json data. PHP's json_encode function only supports utf-8 encoding, so I have no choice but to iconv. The desired effect is to convert the GBK array into a utf-8 array and pass it 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:

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

The result was blank. Later I remembered that the default encoding was set in the configuration file ini_set('default_charset', 'gbk'); It is definitely not easy to use gbk to deserialize utf-8 strings. It should be possible to add ini_set('default_charset', 'utf-8'); between serialization and deserialization. However, since it is a global encoding setting, it can easily lead to encoding problems in other places, such as database operations.

Using the serialization method of constructing the array prototype and using the var_export function, the final function is as follows:

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

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.

Summary: Most of the methods on the Internet use recursive calls to iconv. If the array has too many elements or more dimensions, the performance will be poor. Therefore, I personally feel that the best way is to use native code. There is no need to consider whether it is an N-dimensional array or an associative array. Everything is done automatically to ensure that the data is consistent before and after the array conversion.

This is the introduction to the issue of encoding conversion in PHP arrays. I hope it will be helpful to everyone.



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