Php array encoding conversion method-php Tutorial

Source: Internet
Author: User
Php array encoding conversion method first

$ Data = eval ('Return '. iconv ('gbk', 'utf-8', var_export ($ data, true )).';');

The var_export function can output or return a string representation of a variable. This function returns the structure information about the variables passed to this function. it is similar to var_dump ()

Similarly, the difference is that the returned representation is a valid php code.

You can set the second parameter of the function to TRUE to return the expression of the variable.

Next we will use this feature to convert the array into a string and then iconv and then use the eval function to restore it to an array. The result is displayed correctly.

Method 2

<? Phpclass arrayiconv {static protected $ in; static protected $ out;/*** static method, this method inputs an array and returns the array ** @ param unknown_type $ array input array * @ param unknown_type $ in input array encoding * @ param unknown_type $ out returns the array encoding * @ return array returned by unknown */static public function Conversion ($ array, $ in, $ out) {self ::$ in = $ in; self ::$ out = $ out; return self: arraymyicov ($ array );} /*** internal method, loop array ** @ param unknown_type $ array * @ return unknown */static priva Te function arraymyicov ($ array) {foreach ($ array as $ key => $ value) {$ key = self: myiconv ($ key); if (! Is_array ($ value) {$ value = self: myiconv ($ value);} else {$ value = self: arraymyicov ($ value );} $ temparray [$ key] = $ value;} return $ temparray ;} /*** replace array encoding ** @ param unknown_type $ str * @ return unknown */static private function myiconv ($ str) {return iconv (self ::$ in, self:: $ out, $ str) ;}}$ B = array ("test" => array ("test" => "test", "test" => "test ", "Test" => array ("test" => "test"), "fasdf" => "test", "test" => "test"); $ a = arrayiconv: Conversion ($ B, "UTF-8 "," Gb2312 "); print_r ($ a);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.