php decode is garbled because the "json_encode()" function can only encode UTF-8 characters. The solution is: 1. Before encoding, the Chinese character encoding needs to be converted into UTF-8 format; 2. Just use encode again.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
What should I do if php decode is garbled?
Because the json_encode() function can only encode UTF-8 characters, that is to say, the parameters you pass in to it can only be in UTF-8 format.
So, before you encode, you need to convert your Chinese character encoding into utf-8 format, and then encode again, so there will be no problem.
Related introduction:
json_encode - JSON encoding of variables
Description
json_encode(mixed $value, int $flags = 0, int $depth = 512): string|false
Returns a string, including the representation of value in JSON form. If the argument is an array or object, it will be serialized recursively.
If the value to be serialized is an object, only publicly visible properties are included by default, or the class can implement JsonSerializable to control how its value is serialized to JSON.
The encoding is affected by the passed flags parameter. In addition, the encoding of floating point values depends on serialize_precision.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What to do if php decode is garbled. For more information, please follow other related articles on the PHP Chinese website!