Garbled characters appear when json data is converted into an array in php

PHPz
Release: 2023-05-11 09:20:36
Original
622 people have browsed it

In PHP development, JSON data processing has become a common task. Especially when interacting with the front end or connecting with third-party APIs, JSON is often the most common data format.

In the process of using PHP to process JSON data, sometimes the JSON data is converted into an array and garbled characters appear. This article will explore the reasons for this and how to fix it.

1. Why do garbled codes appear?

In PHP, it is a common operation method to convert JSON data into an array through the json_decode() function. However, if there are Chinese characters in the JSON data, the Chinese characters in the converted array may be garbled.

This is because the default character set used by PHP is ISO-8859-1 instead of UTF-8, so it cannot correctly recognize Chinese characters in UTF-8 encoding format, resulting in garbled characters.

2. Solution

1. Use the second parameter of the json_decode() function

In addition to converting JSON data into an array, the json_decode() function can also Add a second parameter to specify the encoding format of the JSON string.

For example, if the JSON data is UTF-8 encoded, it can be specified through the following code:

$result = json_decode($json, true, JSON_UNESCAPED_UNICODE);
Copy after login

Among them, the third parameter JSON_UNESCAPED_UNICODE tells PHP not to escape Chinese characters, but directly Keep the original UTF-8 encoding.

2. Convert ISO-8859-1 character set to UTF-8 character set

Another solution is to convert ISO-8859-1 character set to UTF-8 character set. This can be achieved by using the iconv() function or the mb_convert_encoding() function.

For example, using the iconv() function you can write like this:

$result = json_decode(iconv("ISO-8859-1", "UTF-8//IGNORE", $json), true);
Copy after login

Among them, the second parameter "UTF-8//IGNORE" tells the iconv() function to encode ISO-8859-1 The characters are converted to UTF-8 encoding.

3. Add the encoding format statement directly to the PHP file

Add the following code at the beginning of the PHP file:

header("Content-type:text/html;charset=utf-8");
Copy after login

This way you can specify the character set of the PHP file as UTF -8 encoding to ensure that Chinese characters can be correctly recognized.

Summary:

If you encounter garbled characters when processing JSON data in PHP, don’t panic. You can add the second parameter of the json_decode() function or convert the ISO-8859-1 encoding. into UTF-8 encoding, or add a character set declaration at the beginning of the PHP file to solve the problem.

The above is the detailed content of Garbled characters appear when json data is converted into an array in php. For more information, please follow other related articles on the PHP Chinese website!

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