Home >Backend Development >PHP Problem >How to solve the problem of garbled data in php json

How to solve the problem of garbled data in php json

藏色散人
藏色散人Original
2021-12-07 09:26:042278browse

php Solution to garbled json data: 1. Convert the encoding through json_encode; 2. Solve the garbled problem through "function json1($array){...}".

How to solve the problem of garbled data in php json

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to solve the php json data garbled problem?

Solution to the problem of PHP returning JSON data and Chinese encoding

When processing the app interface, Chinese becomes \\format after json_encode

If you want to solve the problem of Chinese not being converted when returning to the interface

The first solution

exit(json_encode($result,JSON_UNESCAPED_UNICODE));

The second solution:

 public function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
        static $recursive_counter = 0;
        if (++$recursive_counter > 1000)
        {
            die('possible deep recursion attack');
        }
        foreach ($array as $key => $value)
        {
            if (is_array($value))
            {
                $this->arrayRecursive($array[$key], $function, $apply_to_keys_also);
            }
            else
            {
                $array[$key] = $function($value);
            }
            if ($apply_to_keys_also && is_string($key))
            {
                $new_key = $function($key);
                if ($new_key != $key)
                {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
        $recursive_counter--;
    }
    public function json1($array)
    {
        $this->arrayRecursive($array, 'urlencode', true);
        $json = json_encode($array);
        return urldecode($json);
    }

Call exit ($this->json($result));

How to solve the problem of garbled data in php json

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve the problem of garbled data in php json. 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