Home >Backend Development >PHP Problem >How to convert Chinese to JSON or PHP array

How to convert Chinese to JSON or PHP array

PHPz
PHPzOriginal
2023-04-18 10:19:07689browse

In web development, it is often necessary to return processed data to the front end in the form of JSON or PHP array. Chinese is a common data type, so converting Chinese to JSON or PHP array has become a popular issue in the development process.

This article will introduce you how to convert Chinese into JSON or PHP array to meet the front-end needs.

1. Chinese to JSON

The so-called JSON (JavaScript Object Notation), which is JavaScript object notation, is a lightweight data exchange format. It adopts a syntax style similar to JavaScript, which is easy to read and write, and is also easy to be parsed and generated by machines. JSON is a very useful data transfer format for web development.

In PHP, we can use the json_encode() function to convert a PHP array into a JSON string. However, you need to pay special attention when converting Chinese to JSON, because the json_encode() function uses UTF-8 encoding by default. When the Chinese character set is GBK, garbled characters will appear.

Therefore, when converting Chinese to JSON, you need to convert the Chinese character set to UTF-8 encoding first, and then use the json_encode() function to convert.

The specific code is as follows:

//将中文字符集转换为UTF-8
function convert_encoding($val)
{
    if(is_array($val))
    {
        foreach($val as $k=>$v)
        {
            $val[convert_encoding($k)] = convert_encoding($v);
        }
    }
    elseif(is_string($val))
    {
        if(mb_detect_encoding($val, 'UTF-8', true) === false)
        {
            $val = iconv('GBK', 'UTF-8', $val);
        }
    }
    return $val;
}

//将中文转换为JSON
function chinese_to_json($data)
{
    $data = convert_encoding($data);
    return json_encode($data, JSON_UNESCAPED_UNICODE);
}

In the above code, the convert_encoding() function is used to convert the Chinese character set to UTF-8 encoding, and the chinese_to_json() function is used to convert the converted data Returned in JSON format.

2. Convert Chinese to PHP array

When converting Chinese to PHP array, you also need to consider the Chinese character set. If the Chinese character set is GBK, you need to use the iconv() function to convert it to UTF-8 encoding to avoid garbled characters.

The specific code is as follows:

//将中文字符集转换为UTF-8
function convert_encoding($val)
{
    if(is_array($val))
    {
        foreach($val as $k=>$v)
        {
            $val[convert_encoding($k)] = convert_encoding($v);
        }
    }
    elseif(is_string($val))
    {
        if(mb_detect_encoding($val, 'UTF-8', true) === false)
        {
            $val = iconv('GBK', 'UTF-8', $val);
        }
    }
    return $val;
}

//将中文转换为PHP数组
function chinese_to_array($data)
{
    $data = convert_encoding($data);
    return json_decode($data, true);
}

In the above code, the convert_encoding() function is used to convert the Chinese character set to UTF-8 encoding, and the chinese_to_array() function is used to convert the converted data Returned as a PHP array.

Summary

Converting Chinese to JSON or PHP array is a common data processing operation in web development. When performing such operations, you need to pay attention to the Chinese character set to avoid garbled characters. Through the methods introduced in this article, you can easily convert Chinese data into JSON or PHP arrays to meet the needs of front-end development.

The above is the detailed content of How to convert Chinese to JSON or PHP array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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