Current Location: Home> Latest Articles> PHP Array Encoding Conversion: GBK to UTF-8 Conversion Example and Explanation

PHP Array Encoding Conversion: GBK to UTF-8 Conversion Example and Explanation

gitbox 2025-07-02

PHP Array Encoding Conversion: GBK to UTF-8 Conversion Example

In PHP, the `mb_convert_encoding` function allows you to easily convert the character encoding of an array, whether it's from GBK to UTF-8, or the other way around. This article will show you how to perform these conversions with example code to make it easier to understand.

PHP Array Encoding to GBK

First, we can encode a PHP array containing Chinese characters into the GBK format. Here’s an example:


// Define an array containing Chinese characters
$arr = array('姓名' => '张三', '年龄' => 20, '性别' => '男');

// Convert the array to GBK encoding
$arr_gbk = mb_convert_encoding($arr, 'GBK', 'UTF-8');

// Output the encoded array
print_r($arr_gbk);

This code will convert the array `$arr` to GBK encoding and output the encoded array. The first parameter of the `mb_convert_encoding` function is the array to be converted, the second parameter is the target encoding (GBK in this case), and the third parameter is the original encoding (UTF-8 here).

PHP Array Encoding to UTF-8

Similarly, we can use the `mb_convert_encoding` function to convert an array from GBK encoding to UTF-8 encoding. Here’s an example:


// Define an array containing Chinese characters
$arr = array('姓名' => '张三', '年龄' => 20, '性别' => '男');

// Convert the array to UTF-8 encoding
$arr_utf8 = mb_convert_encoding($arr, 'UTF-8', 'GBK');

// Output the encoded array
print_r($arr_utf8);

This code will convert the array `$arr` to UTF-8 encoding and output the encoded array. The process is similar to the previous example, but the target and original encodings are switched.

Converting GBK Encoded Array to UTF-8 Encoded Array

If you already have a GBK-encoded array and need to convert it to UTF-8 encoding, you can use the following code:


// Define a GBK-encoded array
$arr_gbk = array('姓名' => mb_convert_encoding('张三', 'GBK', 'UTF-8'), '年龄' => 20, '性别' => mb_convert_encoding('男', 'GBK', 'UTF-8'));

// Convert the array encoding to UTF-8
$arr_utf8 = mb_convert_encoding($arr_gbk, 'UTF-8', 'GBK');

// Output the converted array
print_r($arr_utf8);

This code will convert each element in the array `$arr_gbk` from GBK encoding to UTF-8 encoding and output the converted array.

Converting UTF-8 Encoded Array to GBK Encoded Array

If you need to convert a UTF-8 encoded array to GBK encoding, you can use the following code:


// Define a UTF-8 encoded array
$arr_utf8 = array('姓名' => '张三', '年龄' => 20, '性别' => '男');

// Convert the array encoding to GBK
$arr_gbk = mb_convert_encoding($arr_utf8, 'GBK', 'UTF-8');

// Output the converted array
print_r($arr_gbk);

This code will convert each element in the array `$arr_utf8` from UTF-8 encoding to GBK encoding and output the converted array.

Conclusion

By using the `mb_convert_encoding` function, PHP makes it easy to convert arrays between different character encodings, whether it’s from GBK to UTF-8 or the reverse. When working with multilingual applications, correct encoding conversion is essential to ensure accurate data display. We hope this guide has helped you better understand how to perform encoding conversions for PHP arrays.