PHP array to JSON display a code in Chinese, to explain

Source: Internet
Author: User
Tags php array to json php json
This post was last edited by wang23412 on 2013-06-09 17:05:27

PHP JSON Chinese encoding

When I passed the array into JSON format in PHP, I found that Chinese would become a strange encoding. So on the Internet to find a solution, see a lot of people with the following two functions solved:
Function arrayrecursive (& $array, $function, $apply _to_keys_also = False) {    static $recursive _counter = 0;    if (+ + $recursive _counter >) {die        (' possible deep recursion attack ');    }    foreach ($array as $key = + $value) {        if (Is_array ($value)) {            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--;}

function JSON ($array) {arrayrecursive ($array, ' UrlEncode ', true), $json = Json_encode ($array); return UrlDecode ($json);}

I tested the next really can solve the problem, so carefully studied the next code, found the following code does not know what it means:
$array [$key] = $function ($value);

I output $array[$key] before and after this sentence, and I found this sentence to turn Chinese into a string of codes.
Please explain what $function ($value) means.


Reply to discussion (solution)

Cough you can replace ' $function ' with the value of the parameter $function.

Defined
Function arrayrecursive (& $array, $function, $apply _to_keys_also = False)
$array [$key] = $function ($value);

Call
Arrayrecursive ($array, ' UrlEncode ', true);

So $array [$key] = $function ($value);
is $array [$key] = UrlEncode ($value);

Uh.. All right.. Is the parameter passed in. I'm low-end ...

Defined
Function arrayrecursive (& $array, $function, $apply _to_keys_also = False)
$array [$key] = $function ($value);

Call
Arrayrecursive ($array, ' UrlEncode ', true);

So $array [$key] = $function ($value);
is $array [$key] = UrlEncode ($value);

So it seems that the function of these two functions is to first urlencode each value in the array, then json_encode into JSON format, and finally the JSON-formatted string urldecode. Thank you. Look at the code, you can't even turn your head.

This function is for GBK encoded data.
For utf-8 data, this should not be necessary.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.